Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core: JSON Configuration GetSection returns null

I have a file appsettings.json that looks like this:

{
    "MyConfig": {
        "ConfigA": "value",
        "ConfigB": "value"
    }
}

In my Startup.cs I'm building my IConfiguration:

public ConfigurationRoot Configuration { get; set; }

public Startup(ILoggerFactory loggerFactory, IHostingEnvironment environment)
{
      var builder = new ConfigurationBuilder()
                     .SetBasePath(environment.ContentRootPath)
                     .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)                             
                     .AddEnvironmentVariables();

      Configuration = builder.Build();
}

public void ConfigureServices(IServiceCollection services)
{
      //GetSection returns null...
      services.Configure<MyConfig>(Configuration.GetSection("MyConfig"));
}

But Configuration.GetSection("MyConfig") always returns null, although the value exists in my JSON file. Configuration.GetSection("MyConfig:ConfigA") works just fine.

What am I doing wrong?

like image 371
moritzg Avatar asked May 15 '17 07:05

moritzg


4 Answers

For anyone who happens upon this and is trying to do this same thing in a test project, this is what worked for me:

other = config.GetSection("OtherSettings").Get<OtherSettings>();
like image 86
coryrwest Avatar answered Nov 08 '22 01:11

coryrwest


Please refer to this below code

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        var config = Configuration.GetSection("MyConfig");
        // To get the value configA
        var value = config["ConfigA"];

        // or direct get the value
        var configA = Configuration.GetSection("MyConfig:ConfigA");

        var myConfig = new MyConfig();
        // This is to bind to your object
        Configuration.GetSection("MyConfig").Bind(myConfig);
        var value2 = myConfig.ConfigA;
    }
}
like image 45
Herman Avatar answered Nov 08 '22 01:11

Herman


I just ran into this. The values are there if you use the full path, but I needed to auto-bind them to a configuration class.

The .Bind(config) started working after I added auto-property accessors to my class's properties. i.e.

public class MyAppConfig {
  public string MyConfigProperty { get; set;} //this works
  public string MyConfigProperty2; //this does not work
}
like image 4
Alex-v-s Avatar answered Nov 08 '22 00:11

Alex-v-s


This worked for me.

public void ConfigureServices(IServiceCollection services)  
{  
     services.Configure<MyConfig>(con=> Configuration.GetSection("MyConfig").Bind(con));  
}
like image 1
Isuru Amarathunga Avatar answered Nov 08 '22 00:11

Isuru Amarathunga