I'm Trying to Inject my settings from appsettings.json into an Object, i'm following microsoft documentation and my Configuration.GetSection().Get<>() is always null, the documentation that i am using is this one Documentation
I am using .Net Core 2.1
My setting is this:
{
"MongoSettings": {
"ConnectionString": "mongodb://admin:abc123!@localhost",
"Database": "NotesDb"
}
}
My class to inject is this one:
public class MongoSettings
{
public string ConnectionString;
public string Database;
}
and i'm using the following code to fill my injection and it comes always null.
Configuration.GetSection("MongoSettings").Get<MongoSettings>()
Below is the return that i am getting when i'm using the code
My Configuration is ok, look the image below.
What am i doing wrong to my object comes with ConnectionString and Database null? Can someone help me?
Your class is 2 fields. The configuration system only works with properties. Add { get; set; }
to the end of both of the fields to fix it.
public class MongoSettings
{
public string ConnectionString { get; set; }
public string Database { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With