Centralized Configuration Management using Azure App Configuration: Setting Up Dynamic Refresh for Configuration Values

Centralized Configuration Management using Azure App Configuration: Setting Up Dynamic Refresh for Configuration Values

Centralized Configuration Management using Azure App Configuration Series

In the previous article we looked at how we can use Managed Identities to Access Azure App Configuration. In this article we will build on that knowledge. We got the same Music Store application we used previously, and we can setup the application to load configurations dynamically for your application. Configuring this is very easy. All you need to do is configure dynamic refresh in your Program.cs file in the ConfigureAppConfiguration() method.

In AzureAppConfigurationOption there is a method called ConfigureRefresh() which we need to use to configure dynamic refresh. See the following sample code.

using Azure.Identity;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;

namespace MusicStore.Web
{
  public class Program
  {
    ...
    public static IHostBuilder CreateHostBuilder(string[] args) =>
      Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder => {
          webBuilder.UseStartup<Startup>();
        })
        .ConfigureAppConfiguration((context, config) => {
          var settings = config.Build();
          var appConfigEndpoint = settings["AppSettings:AppConfiguration:Endpoint"];
          var userAssignedIdentityClientId = settings["AppSettings:Identity:ClientId"];

          if (!string.IsNullOrEmpty(appConfigEndpoint))
          {
            var endpoint = new Uri(appConfigEndpoint);

            config.AddAzureAppConfiguration(options =>
            {
              options
                .Connect(endpoint, new ManagedIdentityCredential(clientId: userAssignedIdentityClientId))

                // Setting up dynamic app configuration
                .ConfigureRefresh(refreshOpt =>
                {
                  refreshOpt.Register(key: "AppSettings:Version", refreshAll: true, label: LabelFilter.Null);
                })
                .UseFeatureFlags();
            });
          }
        });
  }
}

We can pass in AzureAppConfigurationRefreshOption instance that has Register() method we can use. Here in the Register() method we can supply a key. This key is used as the trigger for the refresh. What you need to do is add this key to the App Configuration instance and then set a value to it. It can be anything. When the value of this key is changed, the App Configuration will trigger a refresh. If the value of this key does not change, regardless if any other app configuration changes, it will not be refreshed.

Example, for the Music Store application I’ve set a configuration value AppSettings:Version on the App Configuration instance and set a value of the key to 1.0. Only when I change this version value of 1.0 to something else like 2.0 the app configuration will be updated.

The Register() method has 2 more parameters. The parameter refreshAll is a Boolean value where it decides whether all the configuration values need to be refreshed or not. We set this to true in our example. The other parameter is label. This is set to one of the values of a label we define on App Configuration. Labels can be used to have multi-dimensional app configurations. If you are using labels, you can set a value for the label to fetch the correct set of configurations marked with that label. For us, we are not using labels for now, so We can set this to an ASCII Null value. LabelFilter.Null is used for that.

Last thing we need to do is to set the AppSettings:Version in the Azure App Configuration instance for Music Store.

1 set refresh trigger value

Run the application and then change the value of the AppSettings:Discount:Amount to a different value and observe that the app is not updated. And only when you update AppSettings:Version value the configuration is pulled down.

Note: You may notice that even if you update the AppSettings:Version value the configuration values are not immediately pulled down. It would take up to 30 seconds to pull down the new configuration values. Let’s look at why.

Setting Cache Expiration Time to Decrease the Time to Update the Configuration Values

App Configuration library has a built-in caching mechanism to caches the app settings values for up to 30 seconds by default. That is why you might not see the values updating immediately. But we can configure this cache time and reduced the time taken to update the configuration.

AzureAppConfigurationRefreshOptions class has a method called SetCacheExpiration() that takes a TimeSpan struct where you can use to reduce the default cache expiration time.

using Azure.Identity;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;

namespace MusicStore.Web
{
  public class Program
  {
    ...
    public static IHostBuilder CreateHostBuilder(string[] args) =>
      Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder => {
          webBuilder.UseStartup<Startup>();
        })
        .ConfigureAppConfiguration((context, config) => {
          var settings = config.Build();
          var appConfigEndpoint = settings["AppSettings:AppConfiguration:Endpoint"];
          var userAssignedIdentityClientId = settings["AppSettings:Identity:ClientId"];

          if (!string.IsNullOrEmpty(appConfigEndpoint))
          {
              var endpoint = new Uri(appConfigEndpoint);

              config.AddAzureAppConfiguration(options =>
              {
                options
                  .Connect(endpoint, new ManagedIdentityCredential(clientId: userAssignedIdentityClientId))

                  // Setting up dynamic app configuration
                  .ConfigureRefresh(refreshOpt =>
                  {
                    refreshOpt.Register(key: "AppSettings:Version", refreshAll: true, label: LabelFilter.Null);

                    // Set the cache expiry time to 10 seconds.
                    refreshOpt.SetCacheExpiration(TimeSpan.FromSeconds(10));
                  })
                  .UseFeatureFlags();
              });
          }
        });
  }
}

Now if you run the application and change the configuration values and the trigger value on the App Configuration instance, you can see the values are pulled down much quicker than before.

Summary

In this article we looked at how we can configure dynamic configuration reload for ASP.Net Core application to make the updated configuration values available for the application. The code sample for this article can be found in the Azure App Configuration Dynamic Config Refresh Example GitHub repository. In the next article we’ll look at how we can configure Offline Caching for App Configuration.

You Might Also Like
Comments