I'm trying to read a list of values from the appsettings.json
file. I'm able to read the Logging
values with no problem, but the list values (i.e Servers
) are null:
appsettings.json:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"Servers": [
"SCHVW2K12R2-DB",
"SCHVW2K12R2-DB\\MSSQL2016",
"SCHVW2K8R2-DB"
]
}
Object Classes:
public class AppSettingsConfiguration
{
public Logging Logging { get; set; }
public Servers Servers { get; set; }
}
//Logging Objects
public class Logging
{
public bool IncludeScopes { get; set; }
public LogLevel LogLevel { get; set; }
}
public class LogLevel
{
public string Default { get; set; }
public string System { get; set; }
public string Microsoft { get; set; }
}
//Server Objects
public class Servers
{
public List<string> Names { get; set; }
}
IOptionsMonitor is a Singleton service that retrieves current option values at any time, which is especially useful in singleton dependencies. IOptionsSnapshot is a Scoped service and provides a snapshot of the options at the time the IOptionsSnapshot<T> object is constructed.
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } private IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { // TODO: Service configuration code here... } public void Configure(IApplicationBuilder app, ...
Application configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read configuration data from key-value pairs using a variety of configuration sources: Settings files, such as appsettings. json.
Try deleting the Servers
class and changing AppSettingsConfiguration
to:
public class AppSettingsConfiguration
{
public Logging Logging { get; set; }
public string[] Servers { get; set; }
}
Servers
is a simple string array, not a complex type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With