I am trying to learn the various ways to retrieve configuration info so I can determine the best path for setting up and using configuration for an upcoming project.
I can access the various single settings using
var sm = new SmsSettings
{
FromPhone = Configuration.GetValue<string>("SmsSettings:FromPhone"),
StartMessagePart = Configuration.GetValue<string>("SmsSettings:StartMessagePart"),
EndMessagePart = Configuration.GetValue<string>("SmsSettings:EndMessagePart")
};
I also need to be able to count settings, determine values of certain settings etc. So I was building a parsing method to do these types of things and needed a whole section of the settings file, which is what I assumed GetSection did. Wrong.
appsettings.json :
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=TestingConfigurationNetCoreTwo;Trusted_Connection=True;MultipleActiveResultSets=true",
"ProductionConnection": "Server=(localdb)\\mssqllocaldb;Database=TestingConfigurationNetCoreTwo_Production;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"SmsSettings": {
"FromPhone": "9145670987",
"StartMessagePart": "Dear user, You have requested info from us on starting",
"EndMessagePart": "Thank you."
}
}
This code:
var section = Configuration.GetSection("ConnectionStrings");
Returns these results:
A few questions arise.
- Why is this returning 3 different JsonConfigurationProviders, one of which includes every setting in the appsettings.json file (shown in Image 2)
- Why isn't GetSection("ConnectionStrings") actuall doing just that, returning the sub children of the ConnectionStrings
- Given number 2, how do you actually just retrieve the children of ConnectionStrings ?
- Assuming a model ConnectionStrings, with one property, List Connections, can the section be converted to an object?