I have multiple sections in my appsettings.json file that in ASP.NET Core 6 application.
{
"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
}
}
And I have created classes for sections.
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; }
}
I want to access all settings using only one interface named IApplciationSettings.
public interface IApplciationSettings
{
OracleSettings OracleSettings { get; set; }
ElasticSettings ElasticSettings { get; set; }
RedisSettings RedisSettings { get; set; }
}
adding the builder services:
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)));
So I want to bind to IApplciationSettings interface all configurations and use it in constructor dependency injections like following:
[Route("api/[controller]")]
[ApiController]
public class ProductController : ControllerBase
{
readonly IProductService productService;
public PoructController(
IApplciationSettings settings, IProductService productService)
{
this.productService = productService;
var oraclesetting = settings.OracleSettings;
}
}
How can I do this?