我正在开发一个 Web 应用程序(.net core 2.2)并尝试替换控制器中对查询字符串参数的现有依赖项。我知道,可以替换 Startup.cs 中的依赖项(ConfigureServices(IServiceCollection services)方法)。但问题是我无法访问控制器中的“IServiceCollection”。你们知道如何实现它吗?
这就是在Startup中替换依赖的方法。 “ServiceCollectionDescriptorExtensions”具有“替换”方法。
services.Replace(ServiceDescriptor.Scoped<IFooService,AnotherVersionOfFooService>());
https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.extensions.servicecollectiondescriptorextensions?view=dotnet-plat-ext-3.1#Microsoft_Extensions_DependencyInjection_Extensions_ServiceCollectionDescriptorExtensions https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.extensions.servicecollectiondescriptorextensions?view=dotnet-plat-ext-3.1#Microsoft_Extensions_DependencyInjection_Extensions_ServiceCollectionDescriptorExtensions
Thanks
像这样的东西...
公开课服务集合
{
public IConfiguration Configuration { get; }
public IServiceCollection Services { get; }
public ServicesCollections()
{
}
public ServicesCollections(IConfiguration configuration, IServiceCollection services)
{
Configuration = configuration;
Services = services;
}
}
/// <summary> Generic singleton with double check pattern and with instance parameter </summary>
/// <typeparam name="T"></typeparam>
public class SingleObject<T> where T : class, new()
{
/// <summary> Lock object </summary>
private static readonly object _lockingObject = new object();
/// <summary> Instance </summary>
private static T _singleObject;
/// <summary> ctor </summary>
public SingleObject()
{
}
/// <summary> Instance with parameter </summary>
/// <param name="param">Parameters</param>
/// <returns>Instance</returns>
public static T Instance(params dynamic[] param)
{
if (_singleObject == null)
{
lock (_lockingObject)
{
if (_singleObject == null)
{
_singleObject = (T)Activator.CreateInstance(typeof(T), param);
}
}
}
return _singleObject;
}
}
在你的启动中——
公共无效ConfigureServices(IServiceCollection服务)
{
services.AddControllersWithViews();
services.Configure<CookiePolicyOptions>(options =>
{
//This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = Microsoft.AspNetCore.Http.SameSiteMode.None;
}
);
services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Latest);
services.AddMvc()
.AddControllersAsServices();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddRazorPages();
var configs = Interfaces.SingleObject<Interfaces.ServicesCollections>.Instance(Configuration, services);
services.AddScoped<ServicesCollections>();
services.AddTransient(ctx => new SSO.Gateway.Web.Controllers.LoginController(configs));
配置身份服务器(服务);
}
在你的控制器中——
public class YourController : Controller
{
public YourController (ServicesCollections obj)
{
Configuration = obj.Configuration;
Services = obj.Services;
ConfigureServices();
}
public IConfiguration Configuration { get; }
public IServiceCollection Services { get; }
public void ConfigureServices()
{
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)