Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration.GetSection always returns Value property null

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.

like image 839
Liam Mueller Avatar asked Sep 02 '17 19:09

Liam Mueller


People also ask

What is IConfiguration C#?

Extension Methods Bind(IConfiguration, Object) Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.

What is IOptions?

IOptionsMonitor is a Singleton service that retrieves current option values at any time, which is especially useful in singleton dependencies. IOptionsSnapshot is a Scoped service and provides a snapshot of the options at the time the IOptionsSnapshot<T> object is constructed.

What is options pattern?

The options pattern provides us with various options to read the config data using strongly types classes. Depending upon service lifetime and recomputation requirements of the config data, one can use IOptions, IOptionsSnapshot, and IOptionsMonitor interfaces to read config data.


2 Answers

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).

like image 174
Ivan Agrenich Avatar answered Oct 02 '22 15:10

Ivan Agrenich


  1. Right-click on appsettings.json and go to Properties.
  2. Select Copy to output directory = Copy always.
like image 29
Anmol Rastogi Avatar answered Oct 02 '22 16:10

Anmol Rastogi