Skip to content

Allow access to child providers in ChainedConfigurationProvider #44486

@avanigupta

Description

@avanigupta

API Proposal

This API proposal provides the ability to enumerate a ChainedConfigurationProvider's child providers:

namespace Microsoft.Extensions.Configuration
{
    public class ChainedConfigurationProvider : IConfigurationProvider, IDisposable
    {
+        /// <summary>
+        /// The chained <see cref="IConfigurationProvider"/> instances.
+        /// </summary>
+        IEnumerable<IConfigurationProvider>? Providers { get; }

Motivation

Developers tend to add their configuration source via a ChainedConfigurationSource/Provider and they would want to access it from the outer ConfigurationRoot.

ConfigurationRoot exposed Providers. With this new API now, configuration providers that are nested under a ChainedConfigurationProvider if the user calls IConfigurationBulder.AddConfiguration(...) would now also be able to be accessed.

Usage

Sample usage can be found in AzureAppConfigurationRefresherProvider under the Azure/AppConfiguration-DotnetProvider repo.

Original Description

We have an issue in Azure App Configuration where AzureAppConfigurationProvider could be nested under a ChainedConfigurationProvider if the user calls IConfigurationBuilder.AddConfiguration(...) to add configuration built from the App Configuration provider.

Sample code snippet of this use case:

public static IWebHost BuildWebHost(string[] args)
{
    var configBuilder = new ConfigurationBuilder();
    IConfiguration configuration = configBuilder.AddAzureAppConfiguration(options =>
    {
        options.Connect(connectionString)
        .ConfigureRefresh(refreshOptions =>
        {
            refreshOptions.Register("Settings:BackgroundColor", refreshAll: true)
                          .SetCacheExpiration(TimeSpan.FromSeconds(10));
        });
    })
    .Build();

    return WebHost.CreateDefaultBuilder(args)
                  .ConfigureAppConfiguration((hostingContext, config) =>
                  {
                      config.AddJsonFile("appsettings.json").Build();
                      config.AddConfiguration(configuration);
                  })
                  .UseStartup<Startup>()
                  .Build();
}

Unfortunately, ChainedConfigurationProvider does not provide a way for others to access its child providers. This caused the issue that the App Configuration middleware could not find the instance of AzureAppConfigurationProvider (because in this case its under ChainedConfigurationProvider) and thus cannot obtain the instance of IConfigurationRefresher, which is responsible for refreshing the application configuration (our relevant middleware code here).

Would it be possible to expose the underlying providers in ChainedConfigurationProvider so that we can extract AzureAppConfigurationProvider and enable users to leverage our dynamic refresh functionality?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions