\n
Pls refer to my code sample, it worked in my side. I created a new .net 6 api project. After running the app, you can change the value in azure portal fot testing the refresh.
\n\nAdd connection string to appsetting.json:
\n"ConnectionStrings": {\n "AppConfig": "Endpoint=https://xxxx.azconfig.io;Id=yaFxxSgH;Secret=5MYxxxs="\n }\n
\nMy program.cs, pls don't forget to add service and middleware, the middleware is used to monitors the sentinel key.
\nusing WebApiNet6AzureAppConfig.Models;\n\nvar builder = WebApplication.CreateBuilder(args);\n\nConfigurationManager configuration = builder.Configuration;\nvar connectionString = builder.Configuration.GetConnectionString("AppConfig");\nbuilder.Host.ConfigureAppConfiguration((hostingContext, config) =>\n{\n var settings = config.Build();\n config.AddAzureAppConfiguration(options =>\n {\n options.Connect(connectionString)\n .ConfigureRefresh(refresh =>\n {\n refresh.Register("TestApp:Settings:FontColor", refreshAll: true)\n .SetCacheExpiration(new TimeSpan(0, 0, 30));\n });\n });\n}).ConfigureServices(services =>\n {\n services.AddControllers();\n });\nbuilder.Services.Configure<AppSettings>(configuration.GetSection("TestApp:Settings"));\nbuilder.Services.AddAzureAppConfiguration();\n\nbuilder.Services.AddControllers();\nbuilder.Services.AddEndpointsApiExplorer();\nbuilder.Services.AddSwaggerGen();\nvar app = builder.Build();\n// Configure the HTTP request pipeline.\nif (app.Environment.IsDevelopment())\n{\n app.UseSwagger();\n app.UseSwaggerUI();\n}\n\napp.UseAzureAppConfiguration();\n\napp.UseHttpsRedirection();\napp.UseAuthorization();\napp.MapControllers();\napp.Run();\n
\nMy test api:
\nusing Microsoft.AspNetCore.Mvc;\nusing Microsoft.Extensions.Options;\nusing WebApiNet6AzureAppConfig.Models;\n\nnamespace WebApiNet6AzureAppConfig.Controllers\n{\n [ApiController]\n [Route("[controller]")]\n public class WeatherForecastController : ControllerBase\n {\n private readonly AppSettings _settings;\n\n public WeatherForecastController(IOptionsSnapshot<AppSettings> settings)\n {\n _settings = settings.Value;\n }\n\n [HttpGet(Name = "GetWeatherForecast")]\n public string Get()\n {\n var res = _settings.Message;\n return res;\n }\n\n }\n}\n
\n","author":{"@type":"Person","name":"Tiny Wang"},"upvoteCount":3}}}Reputation: 33
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 docs.microsoft
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 appsettings.json Configuration file using
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
// Using to connect the azure App configuration
var connectionString = builder.Configuration.GetConnectionString("AppConfig");
builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{
var settings = config.Build();
config.AddAzureAppConfiguration(option =>
{
option.Connect(connectionString).ConfigureRefresh(refresh =>
{
refresh.Register("AAConfiguration:Sentinel", refreshAll:true).SetCacheExpiration(new TimeSpan(0, 0, 30));
});
});
})
.ConfigureServices(service =>
{ service.AddControllers();
});
//Middleware for refreshing the azure App configuration
builder.Services.Configure<AAConfiguration>(builder.Configuration.GetSection("AAConfiguration"));
builder.Services.AddAzureAppConfiguration();
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();
// }
//Middleware for refreshing the azure App configuration
app.UseAzureAppConfiguration();
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": "*"
}
MyConfigServiceCollectionExtensions File
using Microsoft.Extensions.Configuration;
using ReadingConfiguration.Model;
namespace Microsoft.Extensions.DependencyInjection
{
public static class MyConfigServiceCollectionExtensions
{
public static IServiceCollection AddConfig(this IServiceCollection services, IConfiguration configuration)
{
#region This will read the configuration from appsettings.json
services.Configure<TradingConfiguration>(
configuration.GetSection("Trading")
);
services.Configure<CarConfiguration>(
configuration.GetSection("Car")
);
#endregion
// This will read the configuration azure app configuration
services.Configure<AAConfiguration>(
configuration.GetSection("AAConfiguration")
);
return services;
}
}
}
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"
Upvotes: 3
Views: 11649
Reputation: 16076
Pls refer to my code sample, it worked in my side. I created a new .net 6 api project. After running the app, you can change the value in azure portal fot testing the refresh.
Add connection string to appsetting.json:
"ConnectionStrings": {
"AppConfig": "Endpoint=https://xxxx.azconfig.io;Id=yaFxxSgH;Secret=5MYxxxs="
}
My program.cs, pls don't forget to add service and middleware, the middleware is used to monitors the sentinel key.
using WebApiNet6AzureAppConfig.Models;
var builder = WebApplication.CreateBuilder(args);
ConfigurationManager configuration = builder.Configuration;
var connectionString = builder.Configuration.GetConnectionString("AppConfig");
builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{
var settings = config.Build();
config.AddAzureAppConfiguration(options =>
{
options.Connect(connectionString)
.ConfigureRefresh(refresh =>
{
refresh.Register("TestApp:Settings:FontColor", refreshAll: true)
.SetCacheExpiration(new TimeSpan(0, 0, 30));
});
});
}).ConfigureServices(services =>
{
services.AddControllers();
});
builder.Services.Configure<AppSettings>(configuration.GetSection("TestApp:Settings"));
builder.Services.AddAzureAppConfiguration();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAzureAppConfiguration();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
My test api:
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using WebApiNet6AzureAppConfig.Models;
namespace WebApiNet6AzureAppConfig.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly AppSettings _settings;
public WeatherForecastController(IOptionsSnapshot<AppSettings> settings)
{
_settings = settings.Value;
}
[HttpGet(Name = "GetWeatherForecast")]
public string Get()
{
var res = _settings.Message;
return res;
}
}
}
Upvotes: 3