How can I read an array of configuration values using Microsoft.Extensions.Configuration with binding?
For example, given the following XML configuration:
<root>
<eventConfiguration>
<event name="0" source="" type="" target="" />
<event name="1" source="" type="" target="" />
<event name="2" source="" type="" target="" />
</eventConfiguration>
</root>
And the following classes:
public class Configuration
{
public EventConfiguration EventConfiguration {get; set;}
}
public class EventConfiguration
{
public List<Event> Events {get; set;}
}
public class Event
{
public string Name {get; set;}
public string Source {get; set;}
public string Type {get; set;}
public string Target {get; set;}
}
When I try to get an instance of Configuration the Event-List is null:
IConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddXmlFile("default.xml");
var root = builder.Build();
// with binding
var configuration = root.Get<Configuration>();
var events = configuration.EventConfiguration.Events; // null
// without binding
var eventSource = root.GetValue("eventConfiguration:event:0:source", default(string)); // not null