我已经为ASP.NET 7内部部署应用程序设置了Azure应用程序配置(AAC)动态更新。这是工作正常。我希望能够运行一些额外的代码,每当它更新配置虽然。这只会发生在sentinel键更改并且它实际上在本地更新配置时,而不是在AAC中的另一个键更改时。我已经看到了使用EventGrid的潜在方法,但我想知道是否有更简单的方法,我可以做,纯粹从应用程序端。
eoxn13cs1#
您可以尝试使用Microsoft.Extensions.Configuration.AzureAppConfiguration SDK,它将帮助您检测应用配置的更改。首先安装Microsoft.Azure.AppConfiguration.AspNetCore和Microsoft.Extensions.Configuration.AzureAppConfiguration NuGet包。下面是示例代码:Startup.cs:
Microsoft.Azure.AppConfiguration.AspNetCore
Microsoft.Extensions.Configuration.AzureAppConfiguration
public void ConfigureServices(IServiceCollection services) { var builder = new ConfigurationBuilder(); builder.AddAzureAppConfiguration(options => { var settings = Configuration.GetConnectionString("AppConfig"); options.Connect(settings) .ConfigureRefresh(refresh => { refresh.Register(key: "SentinelKey", refreshAll: true); }); }); Configuration = builder.Build(); }
下面的ODE将检测更改:
public void ConfigureServices(IServiceCollection services) { var builder = new ConfigurationBuilder(); builder.AddAzureAppConfiguration(options => { var settings = Configuration.GetConnectionString("AppConfig"); options.Connect(settings) .ConfigureRefresh(refresh => { refresh.Register(key: "SentinelKey", refreshAll: true); }); }); Configuration = builder.Build(); ChangeToken.OnChange( () => Configuration.GetReloadToken(), () => OnConfigurationChanged()); } private void OnConfigurationChanged() { Console.WriteLine("Configuration has been updated!"); }
以下是您可以参考推送或轮询模型的链接:https://learn.microsoft.com/en-us/azure/azure-app-configuration/enable-dynamic-configuration-aspnet-core?tabs=core6x另一种方法是使用IOptionsMonitor,它用于检索选项和管理TOptions示例的选项通知。https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.options.ioptionsmonitor-1?view=dotnet-plat-ext-7.0https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-5.0&source=post_page-----e26b71cfdb2f--------------------------------#options-interfaces-1
1条答案
按热度按时间eoxn13cs1#
您可以尝试使用Microsoft.Extensions.Configuration.AzureAppConfiguration SDK,它将帮助您检测应用配置的更改。
首先安装
Microsoft.Azure.AppConfiguration.AspNetCore
和Microsoft.Extensions.Configuration.AzureAppConfiguration
NuGet包。下面是示例代码:
Startup.cs:
下面的ODE将检测更改:
以下是您可以参考推送或轮询模型的链接:
https://learn.microsoft.com/en-us/azure/azure-app-configuration/enable-dynamic-configuration-aspnet-core?tabs=core6x
另一种方法是使用IOptionsMonitor,它用于检索选项和管理TOptions示例的选项通知。
https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.options.ioptionsmonitor-1?view=dotnet-plat-ext-7.0
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-5.0&source=post_page-----e26b71cfdb2f--------------------------------#options-interfaces-1