我的应用程序设置中有多个部分.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;
}
}
我该怎么做?