如何在一个界面中使用多个部分 asp.net core 6?

How to use multiple sections in one interface asp.net core 6?

我的 appsettings.json 文件中有多个部分在 ASP.NET Core 6 应用程序中。

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "OracleSettings": {
    "Host": "host.com",
    "Port": "1521"
  },
  "ElasticSettings": {
    "Hosts": "elastic.com"
  },
  "RedisSettings": {
    "Ip": "redis.com",
    "Timeout": 1,
    "Duration": 50
  }  
}

而且我已经为部分创建了 类。

public class OracleSettings
{
    public string Host { get; set; }
    public string Port { get; set; }
}

public class ElasticSettings
{
    public string Hosts { get; set; }
}

public class RedisSettings
{
    public string Ip { get; set; }
    public int Timout { get; set; }
    public int Duration { get; set; }
}

我只想使用一个名为 IApplciationSettings.

的界面访问所有设置
public interface IApplciationSettings
{
    OracleSettings OracleSettings { get; set; }
    ElasticSettings ElasticSettings { get; set; }
    RedisSettings RedisSettings { get; set; }
}

添加构建器服务:

builder.Services.Configure<ElasticSettings>(builder.Configuration.GetSection(nameof(ElasticSettings)));
builder.Services.Configure<OracleSettings>(builder.Configuration.GetSection(nameof(OracleSettings)));
builder.Services.Configure<RedisSettings>(builder.Configuration.GetSection(nameof(RedisSettings)));

所以我想将所有配置绑定到 IApplciationSettings 接口,并在构造函数依赖注入中使用它,如下所示:

[Route("api/[controller]")]
[ApiController]
public class ProductController : ControllerBase
{
    readonly IProductService productService;

    public PoructController(
        IApplciationSettings settings, IProductService productService)
    {
        this.productService = productService;
        
        var oraclesetting = settings.OracleSettings;
    }
}

我该怎么做?

您可以根据您的 IApplicationSettings 创建您的 Class,并在程序中编写:

builder.Services.Configure<ApplicationSettings>(builder.Configuration);

然后在你的构造函数中你

[Route("api/[controller]")]
[ApiController]
public class ProductController : ControllerBase
{
    readonly IProductService productService;

    public PoructController(IOptions<ApplicationSettings> settings, IProductService productService)
    {
        this.productService = productService;
        
        var oraclesetting = settings.Value.OracleSettings;
    }
}

假设有IApplciationSettings

的具体实现
public class ApplciationSettings : IApplciationSettings {
    public OracleSettings OracleSettings { get; set; }
    public ElasticSettings ElasticSettings { get; set; }
    public RedisSettings RedisSettings { get; set; }
}

直接从配置中提取设置并将其添加到依赖注入服务容器。

//...

ApplciationSettings settings = builder.Configuration.Get<ApplciationSettings>();

if(settings == null) throw new Exception("message here"); //fail early

builder.Services.AddSingleton<IApplciationSettings>(settings);

//...

ConfigurationBinder.Get<T> 扩展方法绑定并 returns 指定类型。

Attempts to bind the configuration instance to a new instance of type T. If this configuration section has a value, that will be used. Otherwise binding by matching property names against configuration keys recursively.

这将允许 IApplciationSettings 根据需要注入,而不必与 IOptions 紧密耦合。

引用Bind hierarchical configuration