I have defined the following in my appsettings.json...
{
"Environments": [
{
"Name": "One",
"CloudServices": [
"name1",
"name2"
]
},
{
"Name": "Two",
"CloudServices": [
"name3",
"name4"
]
}
]
}
...and the following strongly typed models:
public class EnvironmentModel
{
public string Name { get; set; }
public string[] CloudServices { get; set; }
}
public class EnvironmentsConfig
{
public EnvironmentModel[] Environments { get; set; }
}
I was hoping I could bind this in my Startup like this:
services.Configure<EnvironmentsConfig>(Configuration.GetSection("Environments"));
However, when injected into my controller as IOptions<EnvironmentsConfig> the value is null. Is it possible to bind strongly typed arrays like this? If not, what is the recommended approach? The number of environments may differ between deployments hence my need for an array.
The Environments section of your configuration contains an array of EnvironmentModel, not an EnvironmentsConfig. To fix it you just need to use the root of your config instead of a section. For example:
services.Configure<EnvironmentsConfig>(Configuration);
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