Home > Back-end >  .NET 6 - Unable to read values from Azure App Configuration Service and map to strong-typed class ob
.NET 6 - Unable to read values from Azure App Configuration Service and map to strong-typed class ob

Time:08-03

I have created the Azure App Configuration service in Azure and added some configurations there as
enter image description here

Then I have a .NET 6 web application where I am trying to read the configurations from the azure service and map those to an object using IOptions as:

Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddAzureAppConfiguration(option =>
{
    option.Connect("myconnectionstring")
   .ConfigureRefresh(refresh =>
   {
    refresh.Register("AppSettings:Sentinel", "Development", refreshAll: true)
                       .SetCacheExpiration(new TimeSpan(0, 0, 10));
   });

}, optional: false);   

builder.Services.Configure<DBSettings(builder.Configuration.GetSection("AppSettings:DBSettings"));
builder.Services.AddAzureAppConfiguration();


var app = builder.Build();
app.UseAzureAppConfiguration();
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=home}/{action=privacy}/{id?}");

app.Run(); 

DBSettings.cs

public class DBSettings
{
    public string ConnectionString { get; set; }
    public int DapperTimeout { get; set; }
}    

HomeController.cs

public class AccountController : BaseController
{
    private readonly DBSettings settings;
    public AccountController(IOptions<DBSettings> options)
    {
        settings = options.Value;
    }    
    public async Task<IActionResult> AuthCallback()
    {
        var gg = settings.ConnectionString;
    }
}    

The issue here is that the settings object is always null in the controller. However, It is not if I do it using the appsettings.json file.

appsettings.json

{
  "AppSettings": {
    "DBSettings": {
      "ConnectionString": "Server=tcp:aimoves-dev.database.windows.net,1433;Initial Catalog=AiMoves_Dev;Persist Security Info=False;User [email protected]@aimoves-dev;Password=PtotheWorld@123;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
      "DapperTimeout": "200"
    }
  }
}

CodePudding user response:

I am not sure why Microsoft documentation here has not mentioned this in their documentation but we need to chain Select() on Connect() and specify the key and label filter for it to work.

Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddAzureAppConfiguration(option =>
{
    option.Connect(builder.Configuration.GetValue<string>("AppSettings:Settings"))
    .Select(keyFilter: KeyFilter.Any, builder.Environment.EnvironmentName)
    .ConfigureRefresh(refresh =>
    {
        refresh.Register("AppSettings:Sentinel", builder.Environment.EnvironmentName, refreshAll: true)
                           .SetCacheExpiration(new TimeSpan(0, 0, 10));
    });

}, optional: false);
  • Related