升级到 WebJobs SDK 3.0 已破坏 appsettings 中的重写配置
Upgrading to WebJobs SDK 3.0 has broken overridden configuration in appsettings
我将 WebJobs 项目迁移到 3.0,但 运行 遇到了一个特殊问题。我的项目有一个 appsettings.json 和各种 appsettings.environment.json 文件。
当运行在任何环境下,不是环境设置覆盖基本设置,而是相反:当基本设置和环境设置中存在任何设置时,将使用基本设置。
我尝试了使用 HostBuilder.ConfigureAppConfiguration()
和 HostBuilder.ConfigureHostConfiguration()
的变体,在每种情况下,当我查看 hostInstance.Configuration.Providers
时,我都注意到一些奇怪的事情:[=14 总是有三个实例=],在配置主机时在 ChainedConfigurationProvider
中使用两个,或者在使用应用程序时仅作为主提供程序数组的一部分。第一个永远是我的基地,第二个永远是环境,第三个永远是基地。
所以我认为我的问题是正在添加第三个 JsonConfigurationProvider,但我不知道它是如何添加的。
这是我的 WebJob 中的相关代码:
var envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
?? Environment.GetEnvironmentVariable("ENV")
?? "development";
var builder = new HostBuilder()
.UseEnvironment(envName)
.ConfigureAppConfiguration(b =>
{
b.SetBasePath(Environment.CurrentDirectory)
.AddCommandLine(args, StartupSettings.SwitchMapping)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{envName}.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
});
builder.ConfigureWebJobs((context, b) =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
b.AddTimers();
});
builder.ConfigureLogging((context, b) =>
{
if (false && context.HostingEnvironment.IsDevelopment())
{
b.SetMinimumLevel(LogLevel.Debug);
}
else
{
b.SetMinimumLevel(LogLevel.Information);
b.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning);
}
b.AddConsole();
var applicationInsightsKey = context.Configuration.GetValue<string>("ApplicationInsights:InstrumentationKey");
b.AddApplicationInsights(o => o.InstrumentationKey = applicationInsightsKey);
})
.UseConsoleLifetime();
builder.ConfigureServices((context, services) =>
{
/*...*/
});
return builder.Build();
刚刚弄明白了。
ConfigureWebJobs()
的扩展方法调用 ConfigureAppConfiguration()
并自动添加应用程序设置。因为我是在调用 ConfigureAppConfiguration()
之后调用它的,这就是最后一个 appsettings.json 是基础的原因。
来自 WebJobsHostBuilderExtensions.ConfigureWebJobs 的代码:
builder.ConfigureAppConfiguration(config =>
{
config.AddJsonFile("appsettings.json", optional: true);
config.AddEnvironmentVariables();
});
最糟糕的是,这些似乎都是多余的。
我将 WebJobs 项目迁移到 3.0,但 运行 遇到了一个特殊问题。我的项目有一个 appsettings.json 和各种 appsettings.environment.json 文件。
当运行在任何环境下,不是环境设置覆盖基本设置,而是相反:当基本设置和环境设置中存在任何设置时,将使用基本设置。
我尝试了使用 HostBuilder.ConfigureAppConfiguration()
和 HostBuilder.ConfigureHostConfiguration()
的变体,在每种情况下,当我查看 hostInstance.Configuration.Providers
时,我都注意到一些奇怪的事情:[=14 总是有三个实例=],在配置主机时在 ChainedConfigurationProvider
中使用两个,或者在使用应用程序时仅作为主提供程序数组的一部分。第一个永远是我的基地,第二个永远是环境,第三个永远是基地。
所以我认为我的问题是正在添加第三个 JsonConfigurationProvider,但我不知道它是如何添加的。
这是我的 WebJob 中的相关代码:
var envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
?? Environment.GetEnvironmentVariable("ENV")
?? "development";
var builder = new HostBuilder()
.UseEnvironment(envName)
.ConfigureAppConfiguration(b =>
{
b.SetBasePath(Environment.CurrentDirectory)
.AddCommandLine(args, StartupSettings.SwitchMapping)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{envName}.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
});
builder.ConfigureWebJobs((context, b) =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
b.AddTimers();
});
builder.ConfigureLogging((context, b) =>
{
if (false && context.HostingEnvironment.IsDevelopment())
{
b.SetMinimumLevel(LogLevel.Debug);
}
else
{
b.SetMinimumLevel(LogLevel.Information);
b.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning);
}
b.AddConsole();
var applicationInsightsKey = context.Configuration.GetValue<string>("ApplicationInsights:InstrumentationKey");
b.AddApplicationInsights(o => o.InstrumentationKey = applicationInsightsKey);
})
.UseConsoleLifetime();
builder.ConfigureServices((context, services) =>
{
/*...*/
});
return builder.Build();
刚刚弄明白了。
ConfigureWebJobs()
的扩展方法调用 ConfigureAppConfiguration()
并自动添加应用程序设置。因为我是在调用 ConfigureAppConfiguration()
之后调用它的,这就是最后一个 appsettings.json 是基础的原因。
来自 WebJobsHostBuilderExtensions.ConfigureWebJobs 的代码:
builder.ConfigureAppConfiguration(config =>
{
config.AddJsonFile("appsettings.json", optional: true);
config.AddEnvironmentVariables();
});
最糟糕的是,这些似乎都是多余的。