C# Azure webjobs不会用Azure应用程序设置覆盖appsettings.json
我有一个Azure web作业(C# Azure webjobs不会用Azure应用程序设置覆盖appsettings.json,c#,azure-webjobs,C#,Azure Webjobs,我有一个Azure web作业(.NET Core 2.2),它在启动时从配置中读取一些设置,如下所示: var builder = new HostBuilder() .UseEnvironment(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")) .ConfigureWebJobs() .ConfigureAppConfiguration((hostContext, configApp) =&g
.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();
var builder=new HostBuilder()
.UseEnvironment(Environment.GetEnvironmentVariable(“ASPNETCORE_环境”))
.ConfigureWebJobs()
.ConfigureAppConfiguration((主机上下文,configApp)=>
{
configApp.AddenEnvironmentVariables();
configApp.AddJsonFile(“appsettings.json”,可选:false);
})
.ConfigureLogging((hostingContext,logging)=>
{
logging.AddConsole();
var instrumentationKey=hostingContext.Configuration[“APPINSIGHTS_instrumentationKey”];
如果(!string.IsNullOrEmpty(instrumentationKey))
{
Console.Writeline(instrumentationKey);//问题在于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,然后使用环境变量覆盖