Configuration.GetSection always returns null

2019-06-15 00:09发布

问题:

Every time I call Configuration.GetSection, the Value property of the returned object is always null.

My Startup constructor

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

    this.Configuration = builder.Build();
}

My ConfigureServices method

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<SqliteSettings>(opts => Configuration.GetSection("SqliteSettings").Bind(opts));

    services.AddOptions();

    services.AddMvc();
}

My appsettings.json

{
  "SqliteSettings": {
    "DataSource": "C:\\db.sqlite",
    "NewDatabase": true,
    "Version": 3
  }
}

The class I'm using to define SqliteSettings

public class SqliteSettings
{
    public string DataSource { get; set; }

    public bool? NewDatabase { get; set; }

    public int? Version { get; set; }

    public string Password { get; set; }

    public long? CacheSize { get; set; }

    // More properties
}

I was thinking the JSON might need to have the same amount of properties to match, or is it might be something to do with data type definitions, but maybe those are completely unrelated.

回答1:

Just modify your ConfigureServices method to be like following:

public void ConfigureServices(IServiceCollection services)
{
    services.AddOptions();

    services.Configure<SqliteSettings>(Configuration.GetSection("SqliteSettings"));

    services.AddMvc();
}

and it should work.



回答2:

According to the Microsoft Docs: "When GetSection returns a matching section, Value isn't populated. A Key and Path are returned when the section exists."

If you want to see the values of that section you will need to call the GetChildren() method: Configuration.GetSection("SqliteSettings").GetChildren();

Or you can use: Configuration.GetSection("SqliteSettings").Get<SqliteSettings>(). The JSON does not need to have the same amount of properties to match. Unmatched nullable properties will be set to null and non-nullable unmatched properties will be set to their default value (e.g. int will be set to 0).



回答3:

  1. Right-click on appsettings.json and go to Properties.
  2. Select Copy to output directory = Copy always.


回答4:

var serviceProvider = services.BuildServiceProvider();

must come after all services.Configure calls.



回答5:

In my case for Web Api Core 2.1, I needed Program.cs in the project root folder (the same folder as Startup.cs).



回答6:

This worked for me in a console application:

var connectionString = config["ConnectionStrings:DefaultConnection"]


回答7:

Try add ConfigurationProvider to ConfigurationBuilder.

App configuration is provided from:

  • appsettings.json using the File Configuration Provider.
  • appsettings.{Environment}.json using the File Configuration Provider.


回答8:

If you have an appsettings.Development.json file, make sure the "ConnectionStrings:DefaultConnection" settings are the same in both the appsettings.Development.json and appsettings.json files.