我正在使用 azure 应用程序配置和 azure 功能。
所以我从 azure 应用程序配置中提取所有关键值。但是,如果 azure 函数正在运行并且我从 azure 应用程序配置中对键值进行任何更改,则 azure func 将不会显示更新的值。
下面是我已经实现的代码
在启动.cs
public override void Configure(IFunctionsHostBuilder builder)
{
var environmentName = Environment.GetEnvironmentVariable("AZURE_FUNCTIONS_ENVIRONMENT");
if (string.IsNullOrWhiteSpace(environmentName))
throw new Exception("AZURE_FUNCTIONS_ENVIRONMENT could not be resolved.");
var hostEnvironment = new HostEnvironment { EnvironmentName = environmentName };
var config = SetUpConfiguration(builder.Services, hostEnvironment);
var appSettings = config.Get<TSettings>();
appSettings.AppName = _appName;
builder.Services.AddSingleton<ICorrelationIdProvider>(new FunctionAppCorrelationIdProvider());
builder.Services.AddSingleton<IIdentityProvider>(new FunctionAppIdentityProvider(appSettings.AppName));
builder.Services.AddSingleton<IHostEnvironment>(hostEnvironment);
builder.Services.AddSingleton(appSettings);
}
private IConfiguration SetUpConfiguration(IServiceCollection services, IHostEnvironment environment)
{
var executionContextOptions = services.BuildServiceProvider().GetService<IOptions<ExecutionContextOptions>>().Value;
var configBuilder = new ConfigurationBuilder().Initialize(environment, executionContextOptions.AppDirectory);
var config = configBuilder.Build();
services.AddSingleton<IConfiguration>(new ConfigurationRoot(config.Providers.ToArray()));
return config;
}
在一个助手类中,我编写了以下代码
builder.AddAzureAppConfiguration(appConfigurationOptions =>
appConfigurationOptions
.Connect(new Uri(config["AppConfig:Endpoint"]), tokenCredential)
.Select(ConfigurationKeys.AZURE_FUNC_TEST)
.Select(KeyFilter.Any, environment.EnvironmentName)
.ConfigureRefresh(refreshOptions =>
{
refreshOptions.Register(ConfigurationKeys.AZURE_FUNC_TEST, false);
refreshOptions.SetCacheExpiration(TimeSpan.FromMinutes(1));
})
);
return builder;
在 azure 函数中,我使用下面的代码来获取值
var test = _settings.AzureFuncTest;
但是在这里我没有得到新的值,直到我不重新启动天蓝色的功能。