Alex G. Alex G. - 1 year ago 124
ASP.NET (C#) Question

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

I updated my project from 1.0.0-rc1-final to 1.0.0-rc2-final which is called ASP.NET Core 2 now. This is how I initialize the configuration builder:

var builder = new ConfigurationBuilder().SetBasePath(Environment.GetEnvironmentVariable("ASPNETCORE_CONTENTROOT")).AddJsonFile(file).AddEnvironmentVariables();
IConfiguration configuration = builder.Build();

I know for sure that the initialization is ok because I can do


in the debugger and see all the values in the configuration files in there.

However, if I try to get a whole configuration section like this


it doesn't work. It returns an object no matter what I pass to GetSection. However, the Value field of this object is always null, regardless whether the section exists or not.

Note that this used to work perfectly fine before.
Any clues?

Answer Source

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();

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download