Runtime: Subsequent calls to ConfigureAppConfiguration cannot see previously added Providers

Created on 14 Oct 2020  路  6Comments  路  Source: dotnet/runtime

Description

I'm trying to configure an app where one configuration provider depends on properties loaded by previous configuration providers. Based on the docs, I expect to be able to create a HostBuilder, and call ConfigureAppConfiguration multiple times, where the second call will have access to settings from providers added by the first call, through the HostBuilderContext.Configuration property. However, in practice, the second call only has access to providers added in ConfigureHostConfiguration.

The docs indicate that it should be possible to chain ConfigureAppConfiguration calls:

Sets up the configuration for the remainder of the build process and application. This can be called multiple times and the results will be additive. The results will be available at Configuration for subsequent operations, as well as in Services.
https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.ihostbuilder.configureappconfiguration?view=dotnet-plat-ext-3.1#Microsoft_Extensions_Hosting_IHostBuilder_ConfigureAppConfiguration_System_Action_Microsoft_Extensions_Hosting_HostBuilderContext_Microsoft_Extensions_Configuration_IConfigurationBuilder__

I've created a small repo that shows the behavior I'm seeing in a small example.
https://github.com/bhughes-pbp/ConfigureAppConfiguration

In the image below, I see the expected Providers returned from HostContext.Configuration at Mark A.
Screen Shot 2020-10-14 at 11 58 18 AM

In this next image, I would expect to see the JsonFileProvider available from HostContext.Configuration at Mark B.
Further down, I confirm that the settings from the json file are not visible unless I explicitly Build() the configuration.
Screen Shot 2020-10-14 at 11 59 35 AM

Configuration

I'm using dotnet core 3.1.401 on macos.

Regression?

I haven't tested this in other framework versions.

area-Extensions-Hosting documentation up-for-grabs

Most helpful comment

The doc here is misleading/wrong. The intention of being able to call ConfigureAppConfiguration multiple times is so multiple pieces of an application to add to the configuration setup. (For example, a library can take an IHostBuilder and add the library's specific configuration settings to it.)

The resulting IConfiguration doesn't get created until after all the ConfigureAppConfiguration callbacks have been executed. See:

https://github.com/dotnet/runtime/blob/6072e4d3a7a2a1493f514cdf4be75a3d56580e84/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs#L188-L200

We should fix the docs to be more precise/correct here.

All 6 comments

Tagging subscribers to this area: @eerhardt, @maryamariyan
See info in area-owners.md if you want to be subscribed.

The doc here is misleading/wrong. The intention of being able to call ConfigureAppConfiguration multiple times is so multiple pieces of an application to add to the configuration setup. (For example, a library can take an IHostBuilder and add the library's specific configuration settings to it.)

The resulting IConfiguration doesn't get created until after all the ConfigureAppConfiguration callbacks have been executed. See:

https://github.com/dotnet/runtime/blob/6072e4d3a7a2a1493f514cdf4be75a3d56580e84/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs#L188-L200

We should fix the docs to be more precise/correct here.

Closing as fixed. Thanks @galakt for your contribution.

Thanks :) is there a suggested pattern for the case where some configuration steps rely on configuration from another provider? For instance, when using an AWS SystemsManager, which relies on AWS credentials being already provided from _somewhere_ before it can inject it's config as a ConfigurationProvider.

The feature to add configuration providers that are initialized using values retrieved from previous providers is not currently supported by the framework.

Was this page helpful?
0 / 5 - 0 ratings