使用IOptions 在ASP.NET Core 3.0中检索应用程序设置

问题描述 投票:0回答:2

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

appsettings。[environment。] 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;
        }

我在这里想念什么?

示例I基于此:

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

c# asp.net-core
2个回答
0
投票

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

下一步,set属性上没有MySettings

[另一方面,请勿使用静态实例。它击败了整个DI的目的。而是在需要的地方注入IOptions<AllSettings>

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;
  }
[...]

0
投票

显示的代码未遵循链接示例的使用方式。

虽然不是一种格式,但如果您遵循链接文章中的实际建议,我建议它会起作用

您做了

services.AddOptions();
services.Configure<AllSettings>(Configuration);

虽然示例显示您应该使用Bind方法

结束ASP.NET Core 3.0中第一轮应用程序设置的最后一步是与应用程序的其余部分共享配置POCO类。在ASP.NET Core中,推荐的方法是使用本机的依赖注入层。您需要做的就是将GlobalAppSettings类的新创建(并经过验证)的实例添加为单例。

var appSettings = new AllSettings();
Configuration.Bind(appSettings); //Bind the instance to the built configuration
services.AddSingleton(appSettings);
© www.soinside.com 2019 - 2024. All rights reserved.