I my .net core MVC application, when I read appsettings.json from the HomeController it works but in other classes, the AppSettings objects is never initialized.
Can anyone find out whats wrong?
I've created my custom class AppSettings where property name are mapped to the section in appsettings.json.
I've included this in the Startup.cs.
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
It works in the HomeController:
private static AppSettings AppSettings { get; set; }
public HomeController(IOptions<AppSettings> settings)
{
AppSettings = settings.Value;
}
private void TestMethod()
{
// This works inside the HomeController
var dbString = AppSettings.DataBaseConnectionString;
}
This constructor in BusinessLogic class will however never been called:
private static AppSettings AppSettings { get; set; }
public BusinessLogic(IOptions<AppSettings> settings)
{
AppSettings = settings.Value;
}
private void TestMethod()
{
// This does NOT work inside the BusinessLogic class. Appsettings is null.
var dbString = AppSettings.DataBaseConnectionString;
}