【问题标题】:Retrieving appsettings with IOptions<T> in ASP.NET Core 3.0在 ASP.NET Core 3.0 中使用 IOptions<T> 检索 appsettings
【发布时间】:2019-10-28 22:44:15
【问题描述】:

使用 ASP.NET Core 3.0,我无法检索我的 appsettings.json 的值。

appsettings.[环境.]json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "MySettings": {
    "Option1": "value1",
    "Option2": "value2",
[...]
    }
}

model.cs:

    public class AllSettings
    {
        public MySettings MySettings { get; }

        public static AllSettings Instance = new AllSettings();

        public AllSettings()
        {
            Instance = this;
        }
    }

    public class MySettings
    {
        public string Option1 { get; set; }
        public string Option2 { get; set; }
[...]
    }

Startup.cs:

        public IConfiguration Configuration { get; private set; }

        public Startup(IConfiguration configuration, IWebHostEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();

            Configuration = builder;
        }

        public void ConfigureServices(IServiceCollection services)
        {
[...]
            services.AddOptions();
            services.Configure<AllSettings>(Configuration);
        }

在我的函数中,使用失败并出现空值。

        public string Testing()
        {
            var settings = AllSettings.Instance;
            var option1 = settings.MySettings.Option1; //null
            return option1;
        }

我在这里错过了什么?

我的示例基于:

https://www.red-gate.com/simple-talk/dotnet/net-development/asp-net-core-3-0-configuration-factsheet/

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:

    appsettings.[environment.]json 是您项目中的真实文件名吗? 应该是appsettings.Development.json 或appsettings.Release.json。

    接下来,MySettings 属性上没有 set。

    另一件事 - 不要使用静态实例。它破坏了整个 DI 的目的。相反,在需要的地方注入IOptions&lt;AllSettings&gt;。

    Startup.cs

            public IConfiguration Configuration { get; private set; }
    
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public void ConfigureServices(IServiceCollection services)
            {
    [...]
                services.Configure<AllSettings>(Configuration);
            }
    

    TestController.cs

    [...]
      TestController(IOptions<AllSettings> settings){
        _settings = settings.Value;
      }
    
      [HttpGet(nameof(TestAction))]
      public MySettings TestAction() {
        return _settings.MySettings;
      }
    [...]
    

    【讨论】:

    • 不,文件是appsettings.Development,json,等等,正如你所指出的。添加了一个二传手,没有变化。您将需要扩展最后一点的含义。你能提供一个功能性的例子吗?
    • 谢谢,但是如果您在 ClassA 中有一个 functionA 调用 ClassB 中的 functionB 而 functionB 有 IOptions 参数,这应该如何工作?将参数从一个函数传递到另一个函数似乎需要做很多额外的工作。
    • 你将它注入到每个需要它的类中,而不是传入参数。还有额外的工作,但不是“很多”。 DI 的好处是众所周知的 - 网上有很多资源 - 我不会在这里重复它们。
    • 请注意,这不是主要问题。我认为你应该从一个全新的 asp.net core 3.0 项目开始。您在 Startup 构造函数中的代码是旧的 - 现在在 Program.cs 的默认构建器中配置设置。
    【解决方案2】:

    Startup.cs

    public static ConfigurationSetting RegisterConfiguration(this IServiceCollection services, IConfiguration configuration)
            {
                services.Configure<ConfigurationSetting>(configuration.GetSection("MySettings"));
                var serviceProvider = services.BuildServiceProvider();
                var iop = serviceProvider.GetService<IOptions<ConfigurationSetting>>();
                return iop.Value;
            }
    

    在任何其他类中注入这样的

    public class HomeController : Controller
    {
      private ConfigurationSetting configSetting;
      public HomeController(IOptions<ConfigurationSetting> op)
      {
        configSetting = op.Value;
      }
    }
    

    【讨论】:

      【解决方案3】:

      检查您的appsettings.{env}.json 文件是否配置为复制到输出目录可能是值得的。它应该是Copy always 或Copy if newer。

      您的ConfigurationBuilder 正在env.ContentRootPath 中查找您的配置文件(根据您的代码),因此应该可以从该位置访问这些文件。

      【讨论】:

        猜你喜欢
        • 2019-06-12
        • 1970-01-01
        • 1970-01-01
        • 2017-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多