IHostEnvironment.EnvironmentName VS Environment.GetEnvironmentVariable(“DOTNET_ENVIRONMENT”)

huangapple go评论68阅读模式
英文:

IHostEnvironment.EnvironmentName VS Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")

问题

在一个使用.NET 6构建的ASP.NET Core Web API应用程序中,IHostEnvironment.EnvironmentNameEnvironment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") 是否始终相同?如果不相同,它们在什么情况下会有差异?

要详细说明一下,我的问题是关于.NET注入到Startup构造函数中的IHostEnvironment实例。类似于这样的内容:Startup(IHostEnvironment env)

英文:

In an ASP.NET Core Web API application, built using .NET 6, is IHostEnvironment.EnvironmentName and Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") always the same? If not, under what circumstances can they differ?

To elaborate a bit, my question is about the IHostEnvironment instance which is injected in Startup constructor by the .NET. Something like this Startup(IHostEnvironment env).

答案1

得分: 1

请参阅此处,了解关于环境确定方式的完整文档。

简而言之,环境是通过读取 DOTNET_ENVIRONMENT 和 ASPNETCORE_ENVIRONMENT 两者来确定的。根据你使用的构建器,其中一个可能会优先于另一个。

当使用 WebApplicationBuilder 时,DOTNET_ENVIRONMENT 的值会覆盖 ASPNETCORE_ENVIRONMENT。对于其他主机,比如 ConfigureWebHostDefaults 和 WebHost.CreateDefaultBuilder,ASPNETCORE_ENVIRONMENT 有更高的优先级。

另外,也可以通过命令行参数来提供环境,例如:dotnet run --environment Production

最后,还可以将环境作为构建器的参数提供:

WebApplication.CreateBuilder(new WebApplicationOptions
{
    EnvironmentName = Environments.Staging
}); 

尽管我必须说我从未不得不这样做过。总的来说,考虑到可能的信息来源有多种,最好不要读取环境变量,而是使用 IHostingEnvironment。

最后一点,环境默认为 Production,除非你明确使用默认值,否则你的环境变量不会设置为该值。

英文:

Please see here for the full documentation on how the environment is determined

The gist of it is that both DOTNET_ENVIRONMENT and ASPNETCORE_ENVIRONMENT are read to determine the environment. Depending on which builder you use, one can take precedence over the other.

> The DOTNET_ENVIRONMENT value overrides ASPNETCORE_ENVIRONMENT when WebApplicationBuilder is used. For other hosts, such as ConfigureWebHostDefaults and WebHost.CreateDefaultBuilder, ASPNETCORE_ENVIRONMENT has higher precedence.

Alternatively, it is also possible to provide the environment as command line parameter as such: dotnet run --environment Production

Finally, it is also possible to provide the environment as an argument to the builder itself:

WebApplication.CreateBuilder(new WebApplicationOptions
{
    EnvironmentName = Environments.Staging
}); 

though I have to say I've never had to do that. All in all, considering how many possible sources there are, it's definitely not a great idea to read the environment variable instead of using the IHostingEnvironment.

One last thing. The environment defaults to Production, which your environment variable won't do unless you explicitly use a default.

答案2

得分: 1

IHostEnvironment.EnvironmentName 和 Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") 总是相同吗?

在深入讨论您的主要问题之前,需要简要介绍一下,但我保证这会让您对您感兴趣的内容有所启发。

IHostEnvironment.EnvironmentName:

通常,IHostEnvironment.EnvironmentName 是 asp.net core 6 之前的版本的一部分,它源自 IHostEnvironment 接口,它通过读取 launchSettings.json 中的信息来提供有关应用程序正在运行的托管环境的信息,如下所示:

IHostEnvironment.EnvironmentName VS Environment.GetEnvironmentVariable(“DOTNET_ENVIRONMENT”)

IHostEnvironment.EnvironmentName VS Environment.GetEnvironmentVariable(“DOTNET_ENVIRONMENT”)

Environment.GetEnvironmentVariable():

从 .NET 6 开始,不再使用 IHostEnvironment.EnvironmentName,取而代之的是 Environment.GetEnvironmentVariable(),它也从 launchSettings.json 中检索环境变量名称,这是从 Environment 静态类派生出来的,如下所示:

IHostEnvironment.EnvironmentName VS Environment.GetEnvironmentVariable(“DOTNET_ENVIRONMENT”)

IHostEnvironment.EnvironmentName VS Environment.GetEnvironmentVariable(“DOTNET_ENVIRONMENT”)

因此,IHostEnvironment.EnvironmentName 适用于 .NET 6 之前的版本,而 Environment.GetEnvironmentVariable() 适用于 .NET 6 及以后的版本,两者都提供了应用程序环境信息,例如开发环境、生产环境或预发布环境。

如果它们不相同,那么在什么情况下它们会有差异?

IHostEnvironment.EnvironmentName 中的 DOTNET_ENVIRONMENT:

实际上,根据 DOTNET_ENVIRONMENTASPNETCORE_ENVIRONMENT,我们可能会得到不同的结果。因为在使用 asp.net core 版本早于 6 的情况下,DOTNET_ENVIRONMENT 的值会覆盖 ASPNETCORE_ENVIRONMENT,因此,您仍然可以准确获取环境变量名称,如下所示:

IHostEnvironment.EnvironmentName VS Environment.GetEnvironmentVariable(“DOTNET_ENVIRONMENT”)

Environment.GetEnvironmentVariable() 中的 DOTNET_ENVIRONMENT:

然而,在 asp.net core 6 及更高版本中,与早于 asp.net core 6 的情况不同,它不会按预期运行。因为 ASPNETCORE_ENVIRONMENT 优先级高于 DOTNET_ENVIRONMENT,您将得到完全不同的结果。对于 DOTNET_ENVIRONMENT,您将获得 null EnvironmentVariable,如下所示:

IHostEnvironment.EnvironmentName VS Environment.GetEnvironmentVariable(“DOTNET_ENVIRONMENT”)

注意: 总结一下,它们都适用于在运行时获取应用程序环境变量信息。但根据 DOTNET_ENVIRONMENT 的优先级,它们的值会有所不同。如果您仍然想要了解更多详细信息,可以查看我们的官方文档

英文:

> IHostEnvironment.EnvironmentName and
> Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") always the
> same?

Before jumping into your main question it needs a brief background but I promise it would enlight you what you are curious about.

IHostEnvironment.EnvironmentName:

Generally, IHostEnvironment.EnvironmentName of asp.net core older than 6 which is derived from IHostEnvironment Interface that returns us information about the hosting environment an application is running in by reading information from launchSettings.json as you can see below:

IHostEnvironment.EnvironmentName VS Environment.GetEnvironmentVariable(“DOTNET_ENVIRONMENT”)

IHostEnvironment.EnvironmentName VS Environment.GetEnvironmentVariable(“DOTNET_ENVIRONMENT”)

Environment.GetEnvironmentVariable():

From dotnet 6 IHostEnvironment.EnvironmentName not longer using and it replaced by Environment.GetEnvironmentVariable() which also Retrieves environment variable names from launchSettings.json that is derived from Environment static class as you can see below:

IHostEnvironment.EnvironmentName VS Environment.GetEnvironmentVariable(“DOTNET_ENVIRONMENT”)

IHostEnvironment.EnvironmentName VS Environment.GetEnvironmentVariable(“DOTNET_ENVIRONMENT”)

Therefore, IHostEnvironment.EnvironmentName older than dotnet6 and Environment.GetEnvironmentVariable() of later than dotnet6 both provides us the information of application environment. For instance, whether its dev, prod or stage.

> If not, under what circumstances can they differ?

DOTNET_ENVIRONMENT In IHostEnvironment.EnvironmentName:

Actually, Depending on DOTNET_ENVIRONMENT and ASPNETCORE_ENVIRONMENT we may experience divergent result. Because DOTNET_ENVIRONMENT value overrides ASPNETCORE_ENVIRONMENT when you would used in asp.net core version older than 6 as it overrides; Thus, you still can get the environment variable name exactly. As you can see below:

IHostEnvironment.EnvironmentName VS Environment.GetEnvironmentVariable(“DOTNET_ENVIRONMENT”)

DOTNET_ENVIRONMENT In Environment.GetEnvironmentVariable():

However, it doesn't act accrdingly in asp.net core 6 and later version unlike older than asp.net core 6. Because ASPNETCORE_ENVIRONMENT has higher precedence over DOTNET_ENVIRONMENT and you would ended up completely different result. For DOTNET_ENVIRONMENT you would get null EnvironmentVariable which you can see below:

IHostEnvironment.EnvironmentName VS Environment.GetEnvironmentVariable(“DOTNET_ENVIRONMENT”)

Note: In summary, they both works for getting application environment varibale information while runnning. But denpending on DOTNET_ENVIRONMENT precedence their value differ. If you still would like to know more details on it you could check our official document here

huangapple
  • 本文由 发表于 2023年2月24日 08:13:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定