c#-Azure webjobs不会使用Azure应用程序设置覆盖appsettings.json
作者:互联网
我有一个Azure Web作业(.NET Core 2.2),它在启动时会从配置中读取几个设置,如下所示:
var builder = new HostBuilder()
.UseEnvironment(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"))
.ConfigureWebJobs()
.ConfigureAppConfiguration((hostContext, configApp) =>
{
configApp.AddEnvironmentVariables();
configApp.AddJsonFile("appsettings.json", optional: false);
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConsole();
var instrumentationKey = hostingContext.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
if (!string.IsNullOrEmpty(instrumentationKey))
{
Console.Writeline(instrumentationKey); // <- this always outputs key from appsettings.json, not from Azure Settings
logging.AddApplicationInsights(instrumentationKey);
}
})
.UseConsoleLifetime();
如您所见,appsettings.json文件应该具有APPINSIGHTS_INSTRUMENTATIONKEY密钥,并且在开发环境中可以很好地读取它.
现在,对于生产而言,我想通过在Azure Application Settings Web界面中添加具有相同键的设置来覆盖此APPINSIGHTS_INSTRUMENTATIONKEY键.
但是,当我将Webjob部署到Azure时,它仍然具有来自appsettings.json的旧应用洞察密钥.为了强制我的Web作业具有Azure应用程序设置中的覆盖键,我必须从appsettings.json中删除应用程序见解键.
我的网络作业是否可以使用Azure应用程序设置而不必从appsettings.json中删除密钥?
解决方法:
问题在于Azure App设置是通过环境变量发送的;并且,您首先要加载环境变量,然后使用appsettings.json覆盖:
.ConfigureAppConfiguration((hostContext, configApp) =>
{
configApp.AddEnvironmentVariables();
configApp.AddJsonFile("appsettings.json", optional: false);
})
反转为
.ConfigureAppConfiguration((hostContext, configApp) =>
{
configApp.AddJsonFile("appsettings.json", optional: false);
configApp.AddEnvironmentVariables();
})
它将首先加载您的appsettings.json,然后使用环境变量覆盖.
标签:azure-webjobs,c 来源: https://codeday.me/bug/20191210/2104760.html