I have an API .NET 5 proyect and I have another proyect (library) that is reference from the API.
I have this appsettings.json in the API project:
{
"ConfigurationSettings": {
"ConnectionStrings": {
"DefaultConnection": "connStringDefault",
"Other": "connStringOther"
}
}
}
And this class in the library:
public class ConfigurationSettings
{
public ConnectionStrings ConnectionStrings { get; set; }
}
public class ConnectionStrings
{
public string DefaultConnection { get; set; }
public string Other { get; set; }
}
I want to use this class to search in the appsettings.json, like this:
string ConnString = ConfigurationSettings.ConnectionStrings.DefaultConnection;
But I need this to be with dependency injection, to use like this:
private readonly IEmpleadoRepository _empleadoRepository;
private readonly IMapper _mapper;
private readonly IConfigurationSettings _config;
public GetEmpleadoByIdHandler(IEmpleadoRepository empleadoRepository, IMapper mapper, IConfigurationSettings config)
{
_empleadoRepository = empleadoRepository;
_mapper = mapper;
_config = config;
}
I google it and I cannot find the correct steps.
IConfigurationthat you already get for free? (Microsoft.Extensions.Configuration.Abstractions) And you can map your customConfigurationSettingssection.