使用IISExpress时,未在本地开发计算机上设置ASP.NET Core 3.1 launchSettings.json环境变量 [英] ASP.NET Core 3.1 launchSettings.json environment variables are not set on local development machine when using IISExpress

查看:86
本文介绍了使用IISExpress时,未在本地开发计算机上设置ASP.NET Core 3.1 launchSettings.json环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了一个ASP.NET Core 3.1应用程序的奇怪情况:未设置launchSettings.json中定义的environmentVariable(例如, Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"))null ).

I have stumbled across a strange situation for an ASP.NET Core 3.1 application: environmentVariables defined in launchSettings.json are not set (e.g. Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") is null).

launchsettings.json看起来像这样:

The launchsettings.json looks like this:

{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:60907",
      "sslPort": 44364
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger/index.html",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "MyApp.WebApi": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger/index.html",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }
}

并且我正在使用 IIS Express .

我调查或尝试过的事情:

Things I have investigated or tried:

  1. 将项目拖到另一台机器上,并且按预期方式工作: Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT" "Development" .

在同一台计算机上运行了一个非常相似的项目(实际上是相同的配置),并且按预期工作

Ran a very similar project (virtually the same configuration) on the same machine and it works as expected

删除了除 IIS Express 之外的所有launchsettings.json内容,并且仍然存在相同的问题

Removed all launchsettings.json content except for IIS Express and it still has the same issue

使用其他配置文件启动,并且按预期运行

Launched using another profile and it works as expected

关闭了Visual Studio->删除了.vs文件夹->重新打开VS +重新运行项目.问题仍然存在

Closed Visual Studio -> removed .vs folder -> reopened VS + rerun project. The issue persists

重新启动计算机,问题仍然存在

Restarted the machine, the problem persists

在配置文件中添加了其他环境变量,它们也被忽略

Added other environment variables in the profile and they are also ignored

比较了正在运行的项目与未运行的IISExpress applicationhost.config文件,没有发现任何区别(项目路径除外).

Compared the working projects vs. the non-working project IISExpress applicationhost.config files and did not notice any difference (except for the project paths).

我目前唯一的解决方法是在Program.cs中进行本地更改,并确保我从未提交它,但我正在寻找一个真正的解决方法:

The only workaround I have right now is to make a local change in Program.cs and make sure I never commit it, but I am looking for a real fix:

string originalEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", originalEnv ?? "Development");

我已经没有足够的想法去尝试了.我认为这与该特定计算机上针对该特定应用程序的IIS Express配置有关,但是我在%userprofile%\ My Documents \ IISExpress \ config \ applicationhost.config 中找不到与环境变量有关的任何内容.代码>

I have run out of ideas to try. I assume it is something related to an IIS Express configuration on that particular machine for this particular application, but I could not find anything related to environment variables in %userprofile%\My Documents\IISExpress\config\applicationhost.config

推荐答案

要在@CodeCaster上添加答案,为什么您实际上无法检索它以及幕后情况.是的,您可以使用 IWebHostEnvironment 检索所需的内容,但是环境名称不是asp.net核心设置的环境变量,如果您没有在lauchsettings.json

To add on @CodeCaster answer why you can't actually retrieve it and what happens behind the scenes. Yes you can use IWebHostEnvironment to retrieve what you need but the enviroment name IS NOT an enviroment variable that asp.net core sets it is something it simply reads from if you dont have it in your lauchsettings.json

环境分为三个阶段:

  • 机器
  • 用户
  • 过程

现在,这里的区别在于, Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")始终查看进程级别,而不查看操作系统级别.您当然可以通过使用 GetEnvironmentVariable(String,EnvironmentVariableTarget)进行更改,但这不会对@CodeCaster有所帮助.因此,使用 IWebHostEnvironment 来获取适合您的情况的环境名称,但是您可能已经在计算机级别或其他方面设置了DOTNET_ENVIROMENT,因此请确保了解您的具体需求.

Now the difference here is that Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") looks always at the process level it does not look at the OS level. You can of course change that by using GetEnvironmentVariable(String, EnvironmentVariableTarget) but that wont help you as @CodeCaster mentioned. So use the IWebHostEnvironment to get the enviroment name should be sufficient for your case but you may have set the DOTNET_ENVIROMENT in machine level or something so make sure to know what you specifically need.

但是 IWebHostEnviroment 如何获取它?

这是因为那些环境名称"不是环境变量.ASP.Net Core不会设置任何类型的环境变量.取而代之的是,将其定义为一种方式,您可以说这是一个开发环境,这是一个生产环境.它可以从计算机"或用户环境"变量中读取,但不会创建任何种类的变量.

Its because those "enviroment names" are not enviroment variables. ASP.Net Core does set any kind of enviroment variable. It insteads defines it as a way for you to say ok this is a development enviroment and this is a production one. It can read from the Machine or User Enviroment variables but it does not create any varaibles of some sort.

所以在使用IIS时无法读取这些环境变量的原因是因为这些环境变量根本就不存在.

So the reason why you cant read those enviroment variables while using IIS is because those enviroment variables dont exist in the first place.

此外, IWebHostEnvironment 不能以某种方式神奇地读取环境变量.他们只是从 lauchsettings.json 加载它,因此再次没有环境变量设置.

Also furthermore the IWebHostEnvironment does not somehow magically read the enviroment variables. They simply load it from the lauchsettings.json so again there is no enviroment variable setting.

更具体地在源代码中:

            hostingEnvironment.EnvironmentName =
            options.Environment ??
            hostingEnvironment.EnvironmentName;

它可以通过您定义的选项进行检索,如果尚未定义,则它将保持不变.

It is either retrieved by the options you have defined and if you have not defined it then it will stay the same.

此外,进程环境变量在终止进程后不会继续存在,因此无需重新启动计算机或清理这些变量.(一般评论)

Furthermore process enviroment variables do not persist after terminating the process so there is no need for machine restart or clean up of those variables. (General comment)

这篇关于使用IISExpress时,未在本地开发计算机上设置ASP.NET Core 3.1 launchSettings.json环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆