I'm trying to read some objects from appsettings.json. The json contains this structure:
...
"DevicePool": [
{ "device":"185.215.0.91:9082", "phonePair":["644004268", "644049008"],"enabled": "true" },
{ "device":"185.215.0.92:9083", "phonePair":["644491698", "644935005"],"enabled": "true" }
]
...
And I try to read it like this:
public DevicePoolMgr(IConfiguration configuration, string devicePoolConfigKey)
{
_devices = new List<DevicePoolItem>();
_configuration = configuration;
string adbPath = Startup.AppConfig["AppConstants:AdbPath"];
var valuesSection = _configuration.GetSection(devicePoolConfigKey);
foreach (IConfigurationSection section in valuesSection.GetChildren())
{
bool enabled = section.GetValue<bool>("enabled");
if (!enabled) continue;
string device = section.GetValue<string>("device");
var phoneNumbers = section.GetValue<string[]>("phonePair");
DevicePhonePair phonePair = new DevicePhonePair(phoneNumbers[0], phoneNumbers[1]);
_devices.Add(new DevicePoolItem() {Device = device, PhonePair = phonePair, Enabled = enabled});
}
}
This works, mostly. I can't get the phonePair part. Device gets its value, enabled too but phonePair is null. I have seen other people using this way to read a list of strings from appsettings. So, don't know what could be the reason.
"DevicePool": see this answer