Search code examples
c#.net-coreconfigurationapp-configconfigurationsection

How to parse json settings in appsettings.json into a class in c# .netcore


I have a appsettings.json that look like this

{
  "AppName": "my-app-service",
  "MyModel": {
    "MyList": [{
      "CountryCode": "jp"
    },{
      "CountryCode": "us"
    }]
  }
}

Now, i have a POCO file, (MyList.cs is ommited, it has a single string key countryCode)

public class MyModel
{
    public IList<MyList> MyList;
}

I wish to make it so that MyModel can be injected anywhere in my code, how do I do that? I see that people do this in their setup.cs

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

How ever it looks like when I use IOption<MyModel> in my code in the contructors, I just get null value. Where am I wrong?


Solution

  • You are correct: calling Configure<T> sets up the options infrastructure for T. This include IOptions<T>, IOptionsMonitor<T> and IOptionsSnapshot<T>. In its simplest forms, configuring the value uses an Action<T> or as in your example binding to a specific IConfiguration. You may also stack multiple calls to either form of Configure. This then allows you to accept an IOptions<T> (or monitor/snapshot) in your class' constructor.

    The easiest way to determine if binding to an IConfigurationSection is working as you intend it to is to manually perform the binding and then inspect the value:

    var opts = new MyClass();
    var config = Configuration.GetSection("MyClass");
    // bind manually
    config.Bind(opts);
    // now inspect opts
    

    The above is dependant on the Microsoft.Extensions.Configuration.Binder package which you should already have as a transitive dependency if you are referencing the Options infrastructure.

    Back to your issue: the binder will only bind public properties by default. In your case MyClass.MyList is a field. To get the binding to work you must change it to a property instead.

    If you wanted/needed to bind non-public properties you could pass an Action<BinderOptions>:

    // manually
    var opts = new MyClass();
    var config = Configuration.GetSection("MyClass");
    // bind manually
    config.Bind(opts, o => o.BindNonPublicProperties = true);
    
    // DI/services 
    var config = Configuration.GetSection("MyClass");
    services.Configure<MyClass>(config, o => o.BindNonPublicProperties = true);
    

    Even with BinderOptions there is still no way to bind to fields. Also note that there is varying behavior for things like collection interfaces, arrays and get only properties. You may need to play around a bit to ensure things are binding as you intend.