【发布时间】:2018-10-30 13:08:16
【问题描述】:
我有一个具有appsettings.json 的 ASP.NET Core (2.1) 项目。我使用WebHost.CreateDefaultBuilder()。 appsettings.json 文件在文件属性中有如下配置:
构建操作:内容
复制到输出目录:不要复制
在构建之后,appsettings.json 会以 bin\Debug\netcoreapp2.1\MyProj.runtimeconfig.json 结束。
ASP.NET Core 运行时可以很好地加载它。
我创建了 WebJobs(用于 .Net Core 2.1)并想做同样的事情 - 将 Build Action 设置为 Content 并让它加载。在 Program.cs 的 Main() 中,我有类似
var builder = new HostBuilder()
...
.ConfigureAppConfiguration(b =>
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
b.SetBasePath(Directory.GetCurrentDirectory());
b.AddJsonFile("appsettings.json", false, true);
b.AddJsonFile($"appsettings.{environment}.json", true, true);
b.AddEnvironmentVariables();
// Adding command line as a configuration source
if (args != null)
{
b.AddCommandLine(args);
}
}
但运行时会尝试加载 appsettings.json(而不是 MyWebJobProj.runtimeconfig.json)。所以我不得不将 Build Action 设置为 None 并将 Copy to Output Directory 设置为 Always。
但是,我更喜欢与 ASP.NET Core 中相同的方法——它以某种方式处理文件名转换。尽管WebHost.CreateDefaultBuilder() 中的代码与我在 WebJob 中的代码基本相同。配置中的神奇文件名转换是什么以及为什么它只适用于一种类型的项目?
【问题讨论】:
标签: .net-core asp.net-core-2.1 appsettings