Dynamically Reading Azure App Configuration in .Net core Web App using .Net 6

Sreenivasan Thangaraj 6 Reputation points
2022-02-22T08:37:35.677+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: 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

  1. https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-aspnet-core-app?tabs=core6x
  2. 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.

  1. Create secret manager key for connecting to Azure App Configuration in local machine while development dotnet user-secrets init
  2. Configure connection string of Azure App configuration in secret manager.

dotnet user-secrets set ConnectionStrings:AppConfig "Use your app configure primary connection string"

  1. Add package from Nuget Microsoft.Azure.AppConfiguration.AspNetCore
Azure App Configuration
Azure App Configuration
An Azure service that provides hosted, universal storage for Azure app configurations.
209 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,208 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,309 questions
{count} vote