Dynamically Reading Azure App Configuration in .Net core Web App using .Net 6
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: When I do this IOptionsSnapshot couldn't get the value from Azure App Configuration. I am getting values are null even for the first time.
When we use IConfiguration I could get the values for the first time.
Documentation I am referencing: I referred the documents from learn.microsoft
- https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-aspnet-core-app?tabs=core6x
- https://learn.microsoft.com/en-us/azure/azure-app-configuration/enable-dynamic-configuration-aspnet-core?tabs=core5x
For dynamic update there is no reference available for .Net 6 so I verified in .Net 5 and updated the code for .Net 6
My Environment: Using dot net 6 being run from Visual Studio Enterprise 2022, with the latest nuget package for Microsoft.Azure.AppConfiguration.AspNetCore
My Code :
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"];
}
}
}
AAConfiguration File
namespace ReadingConfiguration.Model
{
public class AAConfiguration
{
public string? Message { get; set; }
public int Sentinel { get; set; }
}
}
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": "*"
}
Used user-secrets for connecting to App Configuration from local machine.
- Create secret manager key for connecting to Azure App Configuration in local machine while development dotnet user-secrets init
- Configure connection string of Azure App configuration in secret manager.
dotnet user-secrets set ConnectionStrings:AppConfig "Use your app configure primary connection string"
- Add package from Nuget Microsoft.Azure.AppConfiguration.AspNetCore