Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core Configuration.GetSection().Get<>() not binding

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

enter image description here

My Configuration is ok, look the image below.

enter image description here

What am i doing wrong to my object comes with ConnectionString and Database null? Can someone help me?

like image 883
Slaters Avatar asked Aug 19 '18 04:08

Slaters


Video Answer


1 Answers

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; }
}
like image 115
Scott Chamberlain Avatar answered Sep 26 '22 14:09

Scott Chamberlain