How do I bind a multi level configuration object using IConfiguration in a .net Core application?
Asked Answered
P

3

11

I'm trying to bind to a custom configuration object which should be populated by an appsettings.json file.

My appsettings looks a bit like:

{
  "Logging": {
    "IncludeScopes": true,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "Settings": {
    "Foo": {
      "Interval": 30,
      "Count": 10
    }
  }
}

The settings classes look like:

public class Settings
{
  public Foo myFoo {get;set;}
}

public class Foo
{
  public int Interval {get;set;}
  public int Count {get;set;}

  public Foo()
  {}

  // This is used for unit testing. Should be irrelevant to this question, but included here for completeness' sake.
  public Foo(int interval, int count)
  {
    this.Interval = interval;
    this.Count = count;
  }
}

When I try to bind the Configuration to an object the lowest level it works:

Foo myFoo = Configuration.GetSection("Settings:Foo").Get<Foo>();

myFoo correctly has an Interval and a Count with values set to 30 and 10 respectively.

But this doesn't:

Settings mySettings = Configuration.GetSection("Settings").Get<Settings>();

mySettings has a null foo.

Frustratingly, if I use the debugger I can see that the necessary data is being read in from the appsettings.json file. I can step down into Configuration => Non-Public Members => _providers => [0] => Data and see all of the information I need. It just won't bind for a complex object.

Puce answered 28/12, 2016 at 21:31 Comment(4)
How does your Settings Class look like?Mournful
How are Foo and Settings defined?Delvecchio
@Delvecchio I've added the class definition.Puce
Your settings property is myFoo but your setting is "Foo"...Mournful
M
10

Your property must match the property names in "appsettings.json".

You must rename your Settings' myFoo property into Foo, because that's property name in the json file.

Mournful answered 28/12, 2016 at 21:49 Comment(1)
Looks like that was the issue. I ended up naming the properties the same as the object to try and minimize confusion. Now I just have to get the dictionaries (not mentioned above) mapping. Thanks.Puce
A
4

I've had the same issue a few times.

I resolved my issue by making sure the class properties had {get; set;}.

Assimilate answered 28/10, 2022 at 19:45 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Droit
H
1

You can also use the JsonProperty(Included in Newtonsoft.Json) Annotation to tell the serializer what to do like below.

public class Settings
{
  [JsonProperty("Foo")]
  public Foo myFoo {get;set;}
}
Harm answered 6/1, 2017 at 16:38 Comment(2)
I've checked your code, sorry it doesn't work this wayGrishilde
You've said that binding with JsonProperty attribute will work but it doesn't. You can look at sample app here github.com/akutyrev/SO-Configuration-binding-problemGrishilde

© 2022 - 2024 — McMap. All rights reserved.