My options in appsettings.json
look like this:
"ApplicationSettings": {
"Value1": "abc",
"Value2": 5,
...
"MaxDOP": [
{
"Region": "A",
"ThreadCount": 20
},
{
"Region": "B",
"ThreadCount": 30
},
...
]
}
I inject it as usual:
services.Configure<Models.AppConfigModel>(Configuration.GetSection("ApplicationSettings"));
The problem for me is how to map the MaxDOP
array so that it will maintain the up-to-date sync via IOptionsMonitor<>
. This question deals with arrays in general, but in my case the Region
attribute is unique, and I would rather like a Dictionary<string, int>
(or possibly Dictionary<string, MaxDopSettings>
in case I will ever have more than one non-key value there) so that I could search by the region code efficiently.
Can it be done?
Change the signature of MaxDOP
from list to dictionary
"MaxDOP": {
"A": {
"ThreadCount": 20
},
"B": {
"ThreadCount": 30
}
}
Dop class:
public class Dop
{
public int ThreadCount { get; set; }
}
Configuring:
services.Configure<Dictionary<string, Dop>>(Configuration.GetSection("ApplicationSettings:MaxDOP"));
Usage:
public HomeController(IOptionsMonitor<Dictionary<string, Dop>> monitor)
{
_monitor = monitor;
Dictionary<string, Dop> a = _monitor.CurrentValue;
}
Change the signature of MaxDOP
from list to dictionary:
appsettings.json:
"ApplicationSettings": {
"Value1": "abc",
"Value2": 5,
"MaxDOP": {
"A": {
"ThreadCount": 20
},
"B": {
"ThreadCount": 30
}
}
}
Poco classes:
public class ApplicationSettings
{
public string Value1 { get; set; }
public int Value2 { get; set; }
public Dictionary<string, Dop> MaxDOP { get; set; }
}
public class Dop
{
public int ThreadCount { get; set; }
}
Configuring:
services.Configure<ApplicationSettings>(Configuration.GetSection("ApplicationSettings"));
Usage:
public HomeController(IOptionsMonitor<ApplicationSettings> monitor)
{
_monitor = monitor;
ApplicationSettings a = _monitor.CurrentValue;
}