Can't get config section after update to ASP.NET Core 2

It turns out that one can no longer do something like:

var allSettingsInSection = configuration.Get(typeof(StronglyTypedConfigSection), sectionName);

Instead, it has to be done like this now:

IConfigurationSection sectionData = configuration.GetSection(sectionName);
var section = new StronglyTypedConfigSection();
sectionData.Bind(section);

Note that it's necessary to include Microsoft.Extensions.Configuration.Binder in project.json.


Just a cleaner version of the accepted answer:

public void ConfigureServices(IServiceCollection services)  
{
    services.Configure<MySettings>(options => Configuration.GetSection("MySettings").Bind(options));
}

Source


In dot net core 2.1 you can do this:

I used nameof here to get the name of the class as a string, rather than use an actual string. This is based on Uwe Kleins reply, it's cleaner.

var myConfigClass = Configuration.GetSection(nameof(MyConfigClass)).Get<MyConfigClass>();

Easily inject your strongly typed configuration as follows:

services.Configure<MyConfigClass>(myConfigClass);