C
C#2y ago
mixels

Properties of IConfigurationSection.Get are null?

I have a very simple test case set up using Microsoft.Extensions.Configuration and Microsoft.Extensions.Configuration.Binder. The config (aopsettings.json):
{
"Foo": "bar"
}
{
"Foo": "bar"
}
And the class:
public class Configuration
{
public string Foo;
}
public class Configuration
{
public string Foo;
}
The configuration builds correctly. I can see it contains the Foo property when debugging. However, when I try to use var test = _configuration.Get<Configuration>(), the Foo property of test is null. I'm confused what is going wrong here. Can anyone spot the mistake?
5 Replies
Tinefol
Tinefol2y ago
@mixels your Foo is not actually a property, rather a field. Perhaps try adding {get; set;} to it?
Cracker
Cracker2y ago
JSON data should contain class name as well. Foo under Configuration section
mixels
mixelsOP2y ago
Configuration is the root though isn't it? Ok I will try that, thanks for the suggestion
Pobiega
Pobiega2y ago
it MUST be properties and it MUST have a public setter
mixels
mixelsOP2y ago
Thanks that did the trick. Got so used to Newtonsoft.Json that I never even suspected it required properties.
Want results from more Discord servers?
Add your server