我的应用程序设置中有多个部分.json文件,该文件在ASP.NET核心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;
    }
}

我该怎么做?

推荐答案

假设有一个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>扩展方法绑定并返回指定的类型.

try 将配置实例绑定到类型为T的新实例.如果此配置节具有值,则将使用该值.否则,通过递归地将属性名与配置键匹配来绑定.

这将允许根据需要注入IApplciationSettings,而无需与IOptions紧密耦合.

参考文献Bind hierarchical configuration

Csharp相关问答推荐

利用.NET 8中的AddStandardResilienceDeliveries和AddStandardHedgingDeliveries实现Resiliency

Monty Hall游戏节目模拟给我50/50的结果

迭代C#List并在数据库中 for each 元素执行SELECT语句—更有效的方式?

`Task`只有在C#中等待时才会运行吗?

C#DateTime.ToString在ubuntu和centos中返回不同的结果

Polly v8—使用PredicateBuilder重试特定的状态代码

如何将不同类型的扩展参数的javascript函数转换成C#风格?

如何通过属性初始化器强制初始化继承记录内的属性?

如何使用自定义负载均衡器管理Ocelot负载均衡器中的多线程和批读取

在try 使用访问服务器上的文件夹时,如何解决CORS错误.NET核心API

我什么时候应该在Dapper中使用Connection.OpenAsync?

如何在C#中正确类型化带有泛型的嵌套类

Azure函数正在返回值列表,但该列表在Chrome中显示为空

为什么我在使用有效令牌的情况下仍未获授权?

Autofac -动态实例化:手动传递构造函数

在C#/ASP.NET Core 7中,什么可能导致POST请求作为GET请求发送

在C#和HttpClient中使用REST API

在C#中通过Matheval使用自定义公式

.NET EF Core Automapper项目到筛选不起作用

处理方法内部过滤与外部过滤