【问题标题】:ServiceCollection IOptions<> won't bind properlyServiceCollection IOptions<> 无法正确绑定
【发布时间】:2021-06-15 10:32:56
【问题描述】:

我正在尝试绑定一些可配置的设置。这些值是通过 app-settings.local.json 提供的。要绑定的模型称为可配置设置。我已尝试遵循一些教程并解决问题:

  1. https://andrewlock.net/how-to-use-the-ioptions-pattern-for-configuration-in-asp-net-core-rc2/
  2. https://github.com/aspnet/Configuration/issues/411
  3. Cannot set Configuration from JSON appsettings file in .NET Core project
  4. ServiceCollection returns null for IOptions even though GetSection is working

我已尝试遵循此处给出的建议并将其应用到我自己的应用程序中。经过 4 小时的尝试,我无法让它工作。要么我缺乏实现这一点所需的基本知识,要么我忽略了一些东西。

我的 ConfigurableSettings 类的结构如下:

    public class ConfigurableSettings
    {
        public AppSettings _AppSettings;
        public DgeSettings _DgeSettings;
        
        public class AppSettings
        {
            [JsonProperty("SoftDelete")] 
            public bool SoftDelete { get; set; }
        }

        public class DgeSettings
        {
            [JsonProperty("zipFileUrl")] 
            public string zipFileUrl { get; set; }

            [JsonProperty("sourceFileUrl")] 
            public string sourceFileUrl { get; set; }
        }
    }

我的 ConfigureServices 结构如下:

    public static void ConfigureServices(IServiceCollection serviceCollection, string[] args)
        {
            var serviceCollection = new ServiceCollection(); 
            
            serviceCollection.AddOptions();
            
            var configuration = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile("app-settings.local.json", true)
                                .AddJsonFile("app-settings.json", false)
                                .Build();

            serviceCollection.Configure<ConfigurableSettings>(options => configuration.GetSection("AppSettings").Bind(options));
            serviceCollection.Configure<ConfigurableSettings>(options => configuration.GetSection("DgeSettings").Bind(options));

            var services = serviceCollection.BuildServiceProvider();
            var options = services.GetService<IOptions<ConfigurableSettings>>();

            serviceCollection.AddLogging(loggingBuilder =>
            {
                loggingBuilder.AddConfiguration(configuration.GetSection("Logging"));
                loggingBuilder.AddConsole();
                loggingBuilder.AddDebug();
            });

            serviceCollection.AddServices(configuration);
            serviceCollection.AddNopCommerceServices(configuration);

            serviceCollection.AddTransient<Comparator>();
            serviceCollection.AddTransient<UpdateManager>();
            serviceCollection.AddTransient<DgeRequestAuthenticator>();
            serviceCollection.AddTransient<ISourceConnector, DgeConnector>();
            serviceCollection.AddTransient<IDestinationConnector, NopCommerceConnector>();
        }

我的app-settings.local.json配置如下:

{
  "AppSettings": {
    "SoftDelete": true
  },

  "DgeSettings": {
    "zipFileUrl" : "www.example-url.com",
    "sourceFileUrl" : "www.example-url.com"
  }
}

当我尝试在类中使用它时,我会在构造函数中调用它,如下所示:

private readonly ConfigurableSettings _settings;
        
        public AlphaBetaService(IOptions<ConfigurableSettings> settings)
        {
            _settings = settings.Value;
        }
    

有人可以帮我找出我做错了什么吗?

【问题讨论】:

标签: c# asp.net-core servicecollection


【解决方案1】:

不确定如何在项目中使用ConfigureServices 方法,实际上Startup.cs 中的ConfigureServices 必须是无参数的,或者只接受IServiceCollection 类型的一个参数。

如下更改您的 ConfigureServices:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        var configuration = new ConfigurationBuilder()
                            .SetBasePath(Directory.GetCurrentDirectory())
                            .AddJsonFile("app-settings.local.json", true)
                            .Build();
        services.Configure<ConfigurableSettings>(configuration);

        //...
    }
}

还可以像下面这样更改您的模型设计:

public class ConfigurableSettings
{
    public AppSettings AppSettings{ get; set; }
    public DgeSettings DgeSettings { get; set; }        
}
public class AppSettings
{
    [JsonProperty("SoftDelete")]
    public bool SoftDelete { get; set; }
}

public class DgeSettings
{
    [JsonProperty("zipFileUrl")]
    public string zipFileUrl { get; set; }

    [JsonProperty("sourceFileUrl")]
    public string sourceFileUrl { get; set; }
}

【讨论】:

  • 这不会解决问题,因为您不能按照您的方式声明您的模型。它会警告已经声明了同名的成员。我通过简化我的模型来解决这个问题。现在它只是具有两个区域而不是两个类的 ConfigurableSettings。
  • 请仔细阅读。 AppSettings 和 DgeSettings 不在 ConfigurableSettings 之外。否则它会警告已经声明了相同的名称。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
相关资源
最近更新 更多