Dynamically update .net core config from Azure App Configuration in .Net 6

Sreenivasan Thangaraj 6 Reputation points
2022-02-21T16:17:02.06+00:00

What I am trying to do: I am attempting to setup Azure App Configuration with a .Net 6 Web API application with a sentinel key in Azure App Configuration, with the goal of being able to change keys in azure, and none of the keys will update in my apps until the sentinel value has changed. In theory, this should allow me to safely hot swap configs.

What my issue is : I am binding the model class with IOptionsSnapshot and in the constructor when it is executed it is not getting the values from App Configuration.

What is working : When I try to get the value from App Configuration using IConfiguration it is working.

Why I am using IOptionsSnapshot : This I am using for dynamically refresh the App configuration values based on the sentinel value changes.

Documentation I am referencing: In the official documentation there is no reference to this method see
doc quickstart .net core - https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-aspnet-core-app?tabs=core6x
dynamic configuration .net core - https://learn.microsoft.com/en-us/azure/azure-app-configuration/enable-dynamic-configuration-aspnet-core?tabs=core5x

There is no documentation available for .Net Core.

My Environment: VS 2022 - .Net 6 - Web API .Net Core

Code
appsettings.Json

{  
  "Logging": {  
    "LogLevel": {  
      "Default": "Information",  
      "Microsoft.AspNetCore": "Warning"  
    }  
  },  
  "MySettings": {  
    "Log": true,  
    "ConnectionStringId": "Default",  
    "Parameters": {  
      "IsProduction": false  
    }  
  },  
  "Trading": {  
    "ChartType": "Monthly",  
    "Function": "Pivot",  
    "RSI": true  
  },  
  "Car": {  
    "Manufacturer": "Fiat",  
    "Model": "Punto",  
    "Year": 2013  
  },  
  "AllowedHosts": "*"  
}  

Program.cs File

using ReadingConfiguration.Model;  
  
var builder = WebApplication.CreateBuilder(args);  
  
  
#region Start reading AppSettings.Json file  
//Reading Configuration file using JSon  
builder.Services.Configure<MySettingsConfiguration>(  
    builder.Configuration.GetSection("MySettings"));  
builder.Services.AddConfig(builder.Configuration);  
#endregion  
// Add services to the container.  
  
#region Code start for connecting the Azure App Configuration  
  
var connectionString = builder.Configuration.GetConnectionString("AppConfig");  
builder.Host.ConfigureAppConfiguration(builder =>  
{  
    builder.AddAzureAppConfiguration(option =>  
    {  
        option.Connect(connectionString).ConfigureRefresh(refresh =>  
        {  
            refresh.Register("Sentinel", true).SetCacheExpiration(new TimeSpan(0, 0, 20));  
        });  
    });  
})  
.ConfigureServices(service =>  
{  
    service.AddControllers();     
});  
  
// Add services to the container.This is commented as getting created when connecting the Azure App configuration in the above line  
// builder.Services.AddControllers();  
#endregion  
  
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle  
builder.Services.AddEndpointsApiExplorer();  
builder.Services.AddSwaggerGen();  
  
var app = builder.Build();  
  
// Configure the HTTP request pipeline.  
// If statement can be removed if we need the swagger only in development  
// if (app.Environment.IsDevelopment())  
// {  
    app.UseSwagger();  
    app.UseSwaggerUI();  
// }  
  
app.UseHttpsRedirection();  
  
app.UseAuthorization();  
  
app.MapControllers();  
  
app.Run();  

ReadingAzureAppConfigurationController File

using Microsoft.AspNetCore.Http;  
using Microsoft.AspNetCore.Mvc;  
using Microsoft.Extensions.Configuration;  
using Microsoft.Extensions.Options;  
using ReadingConfiguration.Model;  
  
namespace ReadingConfiguration  
{  
    [Route("api/[controller]")]  
    [ApiController]  
    public class ReadingAzureAppConfigurationController : ControllerBase  
    {  
        private readonly AAConfiguration _aaConfiguration;  
        private readonly IConfiguration _configuration;   
  
        public ReadingAzureAppConfigurationController(IOptionsSnapshot<AAConfiguration> optionsSnapshot,IConfiguration configuration)  
        {  
            _aaConfiguration = optionsSnapshot.Value;  
            _configuration = configuration;  
        }  
  
        [HttpGet]  
        public string ReadingDynamicAzureAppConfiguration()  
        {  
            return _aaConfiguration.Message;  
        }  
  
        [HttpGet]  
        [Route("ReadingAppConfig")]  
        public string ReadingAzureAppConfiguration()  
        {  
            return _configuration["Message"];  
        }  
    }  
}  

Issue : In Line 25 we will get Message value as Null. But in App Configuration I am having the value.

Azure App Configuration
Azure App Configuration
An Azure service that provides hosted, universal storage for Azure app configurations.
237 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,596 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,995 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.