【问题标题】:Proper way to inject custom configuration注入自定义配置的正确方法
【发布时间】:2018-12-03 15:11:54
【问题描述】:

要解决的问题: 有一个设置列表可以说:

{
   "Kind1":
     {"attr1":"val11"},
     {"attr2":"val12"},
   "Kind2":
     {"attr1":"val21"},
     {"attr2":"val22"},       
}

和.NET Core 2.1中的消费者类(控制器),需要访问上述配置才能使用Kind1或Kind2。

假设相应的类已经在 C# 中定义:

 public class KindSetting
 {
    public string attr1{get;set;}
    public string attr2{get;set;}
 }

现在将配置注入消费者对象的最佳方式是什么。

有没有办法将IConfiguration 实例注入消费者对象并像这样使用它?:

KindSetting kindSetting =_configuration.GetValue<KindSetting>(kindSettingKey);

有没有更好的方法来满足上述要求?

【问题讨论】:

    标签: c# asp.net-core dependency-injection configuration .net-core


    【解决方案1】:

    在 startup.cs 文件中,您可以在 ConfigureServices 方法中进行配置。示例代码如下:

       // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //Need to add following lines
            services.Configure<KindSetting>(Configuration.GetSection("Kind1"));
         }
    

    添加到服务后,你可以在你的类中注入这个配置,如下:

    public class HomeController : Controller
    {
    
        private readonly IOptions<KindSetting> _KindSetting;
        
        public HomeController(IOptions<KindSetting> KindSetting)
        {
            _KindSetting = KindSetting
        }
        
        public void myFunction()
        {
                var mysetting = _KindSetting.Value.attr1
        }
    }
    

    【讨论】:

    • 类中的“Kind1”或“Kind2”需要接收与KindSetting相关的全部配置。
    【解决方案2】:

    添加services.Configure&lt;KindSettings&gt;后,您可以通过添加到您的构造函数来通过DI注入配置。

    IOptionsSnapshot<KindSettings> kindSettingsConfiguration
    

    IOptions<KindSettings> kindSettingsConfiguration
    

    不同之处在于,IOptionsSnapshot 将反映配置文件中的实时更改,而IOptions 用于单例使用。

    评论后编辑:

    假设您的配置文件如下所示:

    {
      "Kind1":
        {"attr1":"val11"},
        {"attr2":"val12"},
      "Kind2":
        {"attr1":"val21"},
        {"attr2":"val22"},       
    }
    

    要成功绑定这个,你需要两个配置类

    public class Kind1Configuration
    {
        public string Attr1 { get; set; }
        public string Attr2 { get; set; }
    }
    
    public class Kind2Configuration
    {
        public string Attr1 { get; set; }
        public string Attr2 { get; set; }
    }
    

    如前所述,连接点只需添加

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<Kind1Configuration>(Configuration.GetSection("Kind1"));
    
        services.Configure<Kind2Configuration>(Configuration.GetSection("Kind2"));
    }
    

    要在控制器中使用它,请将您的 IOptions 添加到构造函数中

    public class TestController(IOptionsSnapshot<Kind1Configuration> kindSettingsConfiguration)
    {
        Kind1Configuration configuration = kindSettingsConfiguration.Value;
    }
    

    希望对你有帮助。

    【讨论】:

    • KindSettings 是什么意思? KindSetting 列表?我想将部分配置传递给对象。
    • 您没有完全展示如何在 ConfigureServices 或 Controller 中使用 IOptions。
    • 能否在投反对票前给出解释。
    【解决方案3】:

    我使用了以下方法,但我不确定是最好的

    在启动类中配置方法:

                services.Configure<List<KindSetting>>(Configuration.GetSection("KindSettingList"));
    

    在消费者对象方面:

       public ConsumerController(IOptions<List<KindSetting>> kindSettingsListAccessor,...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 2022-12-28
      • 1970-01-01
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多