如何获取环境变量设置的 DotNet Core ApplicationName
How to get DotNet Core ApplicationName set by Environment Variable
根据 .NET Core documentation,我应该可以使用环境变量设置应用程序名称。
Environment variable: ASPNETCORE_APPLICATIONKEY
我认为情况并非如此。我将 WebHostDefaults.ApplicationKey
设置添加到 Program.cs
,但我仍然无法用环境变量覆盖它。
private static IWebHost BuildWebHost(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", true)
.AddEnvironmentVariables("ASPNETCORE_")
.AddCommandLine(args)
.Build();
return WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((context, builder) => { builder.ClearProviders(); })
.UseConfiguration(config)
.PreferHostingUrls(true)
.UseStartup<Startup>()
.UseSetting(WebHostDefaults.ApplicationKey, "CustomApplicationName")
.Build();
}
在 startup.cs
中我只看到 "CustomApplicationName" 而不是环境变量。
public class Startup
{
public Startup(IConfiguration configuration, IHostingEnvironment hostingEnvironment)
{
Configuration = configuration;
Log.Information($"Startup of application {hostingEnvironment.ApplicationName} in Environment Mode {hostingEnvironment.EnvironmentName}");
}
}
我也试过在环境变量名中使用双下划线。
我 运行 Mac OS。
我怀疑这是文档 "invented" 但实际上并未实现的内容。
ASP.NET 核心托管在 github。我 did a search. The only place where ASPNETCORE_APPLICATIONKEY
shows up is in the documentation itself. The only issue/PR where it comes up is https://github.com/aspnet/Docs/pull/7493 这是将此环境变量添加到文档并包含此有见地的声明的提交:
Did I just make up ASPNETCORE_APPLICATIONKEY? Is that a thing?
如其他答案中所述,正确的环境变量名称是 ASPNETCORE_APPLICATIONNAME
,并且记录了 here. However, it will not work, even as of .NET Core 3.1. There is a GitHub issue 描述了此错误的详细信息,但本质上, [=11] 中的代码=] 方法将 ApplicationName 设置回其默认值,即程序集的名称。
即使您可以使用 UseSetting()
方法重写它,基于 this related GitHub issue 讨论线程中的警告,我也不会这样做。目前最安全的选择似乎是使用您自己的独立环境变量。
根据 .NET Core documentation,我应该可以使用环境变量设置应用程序名称。
Environment variable: ASPNETCORE_APPLICATIONKEY
我认为情况并非如此。我将 WebHostDefaults.ApplicationKey
设置添加到 Program.cs
,但我仍然无法用环境变量覆盖它。
private static IWebHost BuildWebHost(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", true)
.AddEnvironmentVariables("ASPNETCORE_")
.AddCommandLine(args)
.Build();
return WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((context, builder) => { builder.ClearProviders(); })
.UseConfiguration(config)
.PreferHostingUrls(true)
.UseStartup<Startup>()
.UseSetting(WebHostDefaults.ApplicationKey, "CustomApplicationName")
.Build();
}
在 startup.cs
中我只看到 "CustomApplicationName" 而不是环境变量。
public class Startup
{
public Startup(IConfiguration configuration, IHostingEnvironment hostingEnvironment)
{
Configuration = configuration;
Log.Information($"Startup of application {hostingEnvironment.ApplicationName} in Environment Mode {hostingEnvironment.EnvironmentName}");
}
}
我也试过在环境变量名中使用双下划线。
我 运行 Mac OS。
我怀疑这是文档 "invented" 但实际上并未实现的内容。
ASP.NET 核心托管在 github。我 did a search. The only place where ASPNETCORE_APPLICATIONKEY
shows up is in the documentation itself. The only issue/PR where it comes up is https://github.com/aspnet/Docs/pull/7493 这是将此环境变量添加到文档并包含此有见地的声明的提交:
Did I just make up ASPNETCORE_APPLICATIONKEY? Is that a thing?
如其他答案中所述,正确的环境变量名称是 ASPNETCORE_APPLICATIONNAME
,并且记录了 here. However, it will not work, even as of .NET Core 3.1. There is a GitHub issue 描述了此错误的详细信息,但本质上, [=11] 中的代码=] 方法将 ApplicationName 设置回其默认值,即程序集的名称。
即使您可以使用 UseSetting()
方法重写它,基于 this related GitHub issue 讨论线程中的警告,我也不会这样做。目前最安全的选择似乎是使用您自己的独立环境变量。