【问题标题】:.NET Core - Changing dependency in Controller.NET Core - 更改控制器中的依赖项
【发布时间】:2020-06-11 02:58:51
【问题描述】:

我正在开发一个 Web 应用程序 (.net core 2.2) 并尝试替换控制器中对查询字符串参数的现有依赖项。我知道,可以替换 Startup.cs 中的依赖项(ConfigureServices(IServiceCollection services) 方法)。但问题是我无法访问控制器中的“IServiceCollection”。你们知道如何实现它吗?

这是在 Startup 中替换依赖项的方法。 “ServiceCollectionDescriptorExtensions”具有“替换”方法。

services.Replace(ServiceDescriptor.Scoped<IFooService,AnotherVersionOfFooService>());

https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.extensions.servicecollectiondescriptorextensions?view=dotnet-plat-ext-3.1#Microsoft_Extensions_DependencyInjection_Extensions_ServiceCollectionDescriptorExtensions

谢谢

【问题讨论】:

    标签: replace dependency-injection


    【解决方案1】:

    类似这样的...

    公共类 ServicesCollections {

        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;
        }
    }
    

    在你的创业中 ---

    public void 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()
        { 
    
        }
    

    【讨论】:

    • 这个不能存储服务集合!控制器中的 obj 始终为空!
    猜你喜欢
    • 2021-03-22
    • 2020-02-14
    • 2020-02-18
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 2017-07-18
    • 2018-09-14
    • 1970-01-01
    相关资源
    最近更新 更多