Azure webjobs 不会使用 Azure 应用程序设置覆盖 appsettings.json
Azure webjobs does not override appsettings.json with Azure Application Settings
我有一个 Azure 网络作业 (.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 应用程序设置 Web 界面中添加具有相同密钥的设置来覆盖此 APPINSIGHTS_INSTRUMENTATIONKEY
密钥。
但是,当我将网络作业部署到 Azure 时,它仍然具有来自 appsettings.json
的旧应用洞察密钥。为了强制我的 webjob 具有来自 Azure 应用程序设置的覆盖密钥,我必须从 appsettings.json
.
中删除应用洞察密钥
有没有一种方法可以让我的网络作业使用 Azure 应用程序设置而不必从 appsettings.json
中删除密钥?
问题是 Azure 应用程序设置是通过环境变量发送的;并且,您首先加载环境变量,然后用 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 网络作业 (.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 应用程序设置 Web 界面中添加具有相同密钥的设置来覆盖此 APPINSIGHTS_INSTRUMENTATIONKEY
密钥。
但是,当我将网络作业部署到 Azure 时,它仍然具有来自 appsettings.json
的旧应用洞察密钥。为了强制我的 webjob 具有来自 Azure 应用程序设置的覆盖密钥,我必须从 appsettings.json
.
有没有一种方法可以让我的网络作业使用 Azure 应用程序设置而不必从 appsettings.json
中删除密钥?
问题是 Azure 应用程序设置是通过环境变量发送的;并且,您首先加载环境变量,然后用 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,然后用环境变量覆盖。