【问题标题】:Can't get config section after update to ASP.NET Core 2更新到 ASP.NET Core 2 后无法获取配置部分
【发布时间】:2016-10-31 15:04:49
【问题描述】:

我将我的项目从 1.0.0-rc1-final 更新为 1.0.0-rc2-final,现在称为 ASP.NET Core 2。这就是我初始化配置生成器的方式:

var builder = new ConfigurationBuilder().SetBasePath(Environment.GetEnvironmentVariable("ASPNETCORE_CONTENTROOT")).AddJsonFile(file).AddEnvironmentVariables();
IConfiguration configuration = builder.Build();

我确定初始化没问题,因为我可以做到

configuration.AsEnumerable()

在调试器中查看配置文件中的所有值。

但是,如果我尝试像这样获得整个配置部分

configuration.GetSection(section.Name);

它不起作用。无论我将什么传递给 GetSection,它都会返回一个对象。但是,该对象的 Value 字段始终为 null,无论该部分是否存在。

请注意,这曾经可以很好地工作。 有什么线索吗?

【问题讨论】:

    标签: c# asp.net .net asp.net-web-api configuration


    【解决方案1】:

    事实证明,人们不能再做这样的事情了:

    var allSettingsInSection = configuration.Get(typeof(StronglyTypedConfigSection), sectionName);
    

    相反,现在必须这样做:

    IConfigurationSection sectionData = configuration.GetSection(sectionName);
    var section = new StronglyTypedConfigSection();
    sectionData.Bind(section);
    

    请注意,必须在 project.json 中包含 Microsoft.Extensions.Configuration.Binder

    【讨论】:

    • 只是为那些在 2017 年或以后阅读本文的人提供的更新。从ASP.NET Core 2.0 开始,不需要添加这个额外的 NuGet。
    • 我刚刚做了一个 .Net Core 2.1 应用程序,我仍然需要这个 nuget 包,所以我不相信上面的评论是正确的
    • Liam 所说的 - 它仍在 Microsoft.Extensions.Configuration.Binder 中。但是,如果您引用 Microsoft.AspNetCore.AppMicrosoft.AspNetCore.All,您当然会得到它。
    • 您也可以像这样简单地注入强类型配置: var jwtSettingsSection = Configuration.GetSection(nameof(JwtSettings)); services.Configure(jwtSettingsSection);
    【解决方案2】:

    只是已接受答案的更简洁版本:

    public void ConfigureServices(IServiceCollection services)  
    {
        services.Configure<MySettings>(options => Configuration.GetSection("MySettings").Bind(options));
    }
    

    Source

    【讨论】:

      【解决方案3】:

      在 dot net core 2.1 中,您可以这样做:

      我在这里使用 nameof 来获取类的名称作为字符串,而不是使用实际的字符串。这是基于 Uwe Kleins 的回复,它更干净。

      var myConfigClass = Configuration.GetSection(nameof(MyConfigClass)).Get<MyConfigClass>();
      

      如下所示轻松注入强类型配置:

      services.Configure<MyConfigClass>(myConfigClass);
      

      【讨论】:

      • 我喜欢var MyConfigClass = Configuration.GetSection(nameof(MyConfigClass)).Get&lt;MyConfigClass&gt;(); 来摆脱字符串。
      【解决方案4】:

      我正在使用 GetSection 分配,因此我创建了一个扩展方法来帮助我使用泛型获取部分

      public static class ConfigurationExtensions
      {
          public static T GetConfig<T>(this IConfiguration config) where T : new()
          {
              var settings = new T();
              config.Bind(settings);
              return settings;
          }
      
          public static T GetConfig<T>(this IConfiguration config, string section) where T : new()
          {
              var settings = new T();
              config.GetSection(section).Bind(settings);
              return settings;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-08-04
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        • 2021-08-05
        • 1970-01-01
        • 1970-01-01
        • 2018-12-06
        • 2017-03-16
        相关资源
        最近更新 更多