IOptions Injection

前端 未结 5 1756
情书的邮戳
情书的邮戳 2020-12-08 19:17

It seems to me that it\'s a bad idea to have a domain service require an instance of IOptions to pass it configuration. Now I\'ve got pull additional (unnecessary?) depend

5条回答
  •  佛祖请我去吃肉
    2020-12-08 19:57

    credit

    With this two simple lines in startup.cs inside ConfigureServices you can inject the IOptions value like:

    public void ConfigureServices(IServiceCollection services)
    {
        //...
        services.Configure(Configuration.GetSection("AppSettings"));
        services.AddScoped(cfg => cfg.GetService>().Value);
    }
    

    And then use with:

     public MyConnectionResolver(AppSettings appSettings)
     {
           ... 
     }
    

提交回复
热议问题