Azure/AppConfiguration-DotnetProvider

Configuration refresh does not work when ChainedConfigurationProvider is used

zhenlan opened this issue · 8 comments

Problem

A user added the App Configuration middleware to their application, but the configuration is not refreshed.

Cause

The AzureAppConfigurationProvider could be nested under a ChainedConfigurationProvider if the user calls IConfigurationBulder.AddConfiguration(...) to add configuration built from the App Configuration provider.

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 and thus cannot obtain the instance of IConfigurationRefresher.

Proposal

There is no good solution to this. One idea from @drago-draganov is we provide an alternative API for users to call (the name is to be finalized)

public static IConfigurationBuilder AddConfiguration2(this IConfigurationBuilder configurationBuilder, IConfiguration config);

Under the cover, this API will add a type derived from ChainedConfigurationProvider and this type we write exposes child providers.

As part of fixing #166, we can provide additional information and suggest users to call this new API in the exception message.

Reference

The code of ChainedConfigurationProvider can be found at
https://github.com/dotnet/runtime/tree/master/src/libraries/Microsoft.Extensions.Configuration/src

cc: @drago-draganov @abhilasharora @jimmyca15

This is not a viable solution in .NET 6 as the user is not the one calling IConfigurationBulder.AddConfiguration. The framework uses ChainedConfigurationProvider by default.

I also can't get the refresh to work with .NET 7 RC2 and the new minimal API-style Program.cs. I also have a ChainedConfigurationProvider in my list of providers:

image

Is there any workaround for this?

Please add App Configuration provider directly to your configuration builder instead of nested under the ChainedConfigurationProvider. Here is a code sample for .NET 6.

https://learn.microsoft.com/en-us/azure/azure-app-configuration/enable-dynamic-configuration-aspnet-core?tabs=core6x#reload-data-from-app-configuration

Thank you for the quick response! I have been using that link for my implementation.

I did some more quick testing and the ChainedConfigurationProvider is already there from the beginning:
image

And here's how it looks after AddAzureAppConfiguration():
image

The AzureAppConfigurationProvider has been added to the list and isn't a child of ChainedConfigurationProvider.

Anything else I can do?

Is this a web app? If so, you should also add App Configuration middleware as instructed in the same document. It will take care of the configuration refresh.

If it's a console app, you will need to call TryRefreshAsync in your code to get the configuration refreshed. You can find more details in this doc for a console app.
https://learn.microsoft.com/en-us/azure/azure-app-configuration/enable-dynamic-configuration-dotnet-core

I'm so sorry, I seem to have just stopped reading after the step to add builder.Services.AddAzureAppConfiguration() and I haven't added the middleware. One shouldn't program late at night 😪

thank you very much for your help, it is working now.

Now that the ChainedConfigurationProvider exposes its IConfiguration as per dotnet/runtime#44486, is this issue able to be fixed?

Yes, but that's only available in .NET 7.

@avanigupta, we need to code this so it will "light up" when running in a .NET 7 app.