Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

IConfiguration.Get return default property value for IReadOnlyCollection

I got a strange behaviour when try to get collection in configuration from appsettings.json.

My Class:

public record SomeOptions
{
    public IReadOnlyCollection<string> SomeColl { get; init; } = new List<string> { "wrongString" };
}

My appsettings.json:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "Some": {
    "SomeColl": [ "expectedString" ]
  }
}

Then I try get values like this:

var section = configuration.GetSection("Some");
var options = section.Get<SomeOptions>();

I got default value from property (wrongString). But if change property to be like:

public record SomeOptions
{
    public IReadOnlyCollection<string> SomeColl { get; init; } = null!;
}

Or

public record SomeOptions
{
    public ICollection<string> SomeColl { get; init; } = new List<string>();
}

I got value from appsettings.json (expectedString). Can someone explain please – why this is happenning?

>Solution :

This part of the source code covers that:

if (config.GetChildren().Any())
{
    // for arrays and read-only list-like interfaces, we concatenate on to what is already there, if we can
    if (type.IsArray || IsImmutableArrayCompatibleInterface(type))
    {
        if (!bindingPoint.IsReadOnly)
        {
            bindingPoint.SetValue(BindArray(type, (IEnumerable?)bindingPoint.Value, config, options));
        }

        // for getter-only collection properties that we can't add to, nothing more we can do
        return;
    }

for arrays and read-only list-like interfaces, we concatenate on to what is already there, if we can

Though I could not find this being documented.

My guess that this is done to support hierarchical configs (i.e. when array elements are defined in multiple configuration sources, though not sure that this is also a good idea).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading