I need access to the appsettings.json configuration in other non-controller classes in my project. Is there a good way for IConfiguration to be available globally throughout my application w/o using DI? If the IConfiguration DI could be used on non-controller classes, I'd like to know how to do that.
This is my controller where HostService has access to the IConfiguration injection.
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<HostService>();
}).UseSerilog();
}
I need access to IConfiguration in this non-controller class. I'm not sure how to use the DI w/ non-controller type classes, so maybe using a static type configuration variable would be a better solution to make IConfiguration available globally.
public class myTestClass
{
private readonly IConfiguration _config
public myTestClass(IConfiguration config){ _config = config}
}