Hooking into Azure App Configuration Change to Trigger Additional Functionality

172 views Asked by At

I've set up Azure App Configuration (AAC) dynamic updating for an ASP.NET 7 on-prem app. This is working fine. I'd like to be able to run some additional code whenever it updates the config though. This would only happen when the sentinel key changes and it actually updates the config locally, not whenever another key changes in AAC.

I've seen potential ways using EventGrid, but I'm wondering if there's something simpler that I can do, purely from the app side.

1

There are 1 answers

0
Jalpa Panchal On

You could try using the Microsoft.Extensions.Configuration.AzureAppConfiguration SDK that will help you detect change from the app configuration.

First install the Microsoft.Azure.AppConfiguration.AspNetCore and Microsoft.Extensions.Configuration.AzureAppConfiguration NuGet packages.

Here is the sample code:

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    var builder = new ConfigurationBuilder();
    builder.AddAzureAppConfiguration(options =>
    {
        var settings = Configuration.GetConnectionString("AppConfig");
        options.Connect(settings)
               .ConfigureRefresh(refresh =>
               {
                   refresh.Register(key: "SentinelKey", refreshAll: true);
               });
    });
    Configuration = builder.Build();
}

Below ode will detect the changes:

public void ConfigureServices(IServiceCollection services)
{
    var builder = new ConfigurationBuilder();
    builder.AddAzureAppConfiguration(options =>
    {
        var settings = Configuration.GetConnectionString("AppConfig");
        options.Connect(settings)
               .ConfigureRefresh(refresh =>
               {
                   refresh.Register(key: "SentinelKey", refreshAll: true);
               });
    });
    Configuration = builder.Build();

    ChangeToken.OnChange(
        () => Configuration.GetReloadToken(),
        () => OnConfigurationChanged());
}

private void OnConfigurationChanged()
{
    Console.WriteLine("Configuration has been updated!");
}

Here is the link you can refer for push or poll model:

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

Another ways is using the IOptionsMonitor, it is use to retrieve options and manage options notifications for TOptions instances.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.options.ioptionsmonitor-1?view=dotnet-plat-ext-7.0

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-5.0&source=post_page-----e26b71cfdb2f--------------------------------#options-interfaces-1