Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

configuration.AsEnumerable()

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

configuration.GetSection(section.Name);

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?

like image 292
Alex G. Avatar asked Jun 29 '16 04:06

Alex G.


1 Answers

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.

like image 149
Alex G. Avatar answered Sep 24 '22 14:09

Alex G.