简体   繁体   English

为什么存在 aspNetCore web.config 部分时,launchSettings.json 配置文件中的 environmentVariables 不加载?

[英]Why do environmentVariables from launchSettings.json profiles not load when aspNetCore web.config sections exist?

We recently upgraded a .NET Core 2.1 Web App to .NET 3.1.我们最近将 .NET Core 2.1 Web App 升级到 .NET 3.1。 Ever since, our profiles in launchSettings.json are not properly working.从那以后,我们在 launchSettings.json 中的配置文件无法正常工作。 Specifically, the environmentVariables are not loading, with one in particular being "ASPNETCORE_ENVIRONMENT".具体来说,环境变量没有加载,特别是“ASPNETCORE_ENVIRONMENT”。 We use this to switch appSettings.{Environment}.json when debugging locally to target different environments.在本地调试以针对不同环境时,我们使用它来切换 appSettings.{Environment}.json。

In my research, it seems that the web.config is the culprit for this issue.在我的研究中,似乎 web.config 是这个问题的罪魁祸首。 For context, we keep a web.config in our project so we can customize some things for our deployments.作为上下文,我们在项目中保留了 web.config,以便我们可以为我们的部署定制一些东西。 The profiles in question use IISExpress, therefore the web.config is taken into consideration.有问题的配置文件使用 IISExpress,因此考虑了 web.config。

If I remove the web.config, I can switch launch profiles and they take effect.如果我删除 web.config,我可以切换启动配置文件并且它们生效。 However, when a web.config is present, the launch profile environment variables do not take effect.但是,当 web.config 存在时,启动配置文件环境变量不会生效。

Now even more curious, if I remove just this section from the web.config, the environment variables from launch settings start to work again:现在更奇怪的是,如果我从 web.config 中删除这一部分,启动设置中的环境变量将再次开始工作:

在此处输入图像描述

This may be related to the Hosting Model changes going from OutOfProcess (.NET 2.1 default) to InProcess (.NET 3.1 default).这可能与 Hosting Model 从 OutOfProcess(.NET 2.1 默认)更改为 InProcess(.NET 3.1 默认)有关。 In fact, if we force the hostingModel for the project to OutofProcess, the launch profiles work as well, but we'd rather keep InProcess if possible.事实上,如果我们强制项目的 hostingModel 为 OutofProcess,启动配置文件也可以工作,但如果可能,我们宁愿保留 InProcess。

So what's going on here?那么这里发生了什么? Are we missing some migration step?我们是否缺少一些迁移步骤? We followed all the steps on MSDN, starting here and working up to 3.1: https://docs.microsoft.com/en-us/aspnet/core/migration/21-to-22?view=aspnetcore-3.1&tabs=visual-studio我们遵循 MSDN 上的所有步骤,从这里开始直到 3.1: https://docs.microsoft.com/en-us/aspnet/core/migration/21-to-22?view=aspnetcore-3.1&tabs=visual -工作室

Reproduction Steps复制步骤

  1. Create a new ASP.NET Core Web Application, 3.1创建一个新的 ASP.NET 内核 Web 应用程序,3.1
  2. Create a launchSettings.json profile that uses IISExpress, and has "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "SomeTestValue" }创建一个使用 IISExpress 的 launchSettings.json 配置文件,并具有"environmentVariables": { "ASPNETCORE_ENVIRONMENT": "SomeTestValue" }
  3. Run that launch profile and check the environment value on IWebHostEnvironment in Configure of Startup.cs.运行该启动配置文件并检查 Startup.cs 的ConfigureIWebHostEnvironment上的环境值。
  4. Note that the env.EnvironmentName should match "SomeTestValue".请注意, env.EnvironmentName应与“SomeTestValue”匹配。
  5. Now add the following web.config file to your project:现在将以下 web.config 文件添加到您的项目中:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <remove name="aspNetCore" />
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" stdoutLogEnabled="false" arguments="%LAUNCHER_ARGS%" hostingModel="inprocess" />
  </system.webServer>
</configuration>
  1. Run the same profile and check the environment value.运行相同的配置文件并检查环境值。 It should be your machines default value ("Production" in most cases, which is the default when none is found/specified).它应该是您的机器默认值(在大多数情况下为“生产”,当没有找到/指定时这是默认值)。

FIX: Update Visual Studio 2019 to v16.6.3 (Released 1 day earlier!)修复:将 Visual Studio 2019 更新到 v16.6.3(提前 1 天发布!)

在此处输入图像描述

Holy moley, it was Visual Studio.天哪,它是 Visual Studio。 I was running VS 2019 16.6.2 and I JUST got the notification to update today to 16.6.3, but postponed it.我正在运行 VS 2019 16.6.2 ,我刚刚收到今天更新到 16.6.3 的通知,但推迟了它。 Well, after researching all over stack, github, msdn, etc, I found these posts that lead me to see it was a bug in the 16.6.2 build of VS (possibly earlier versions as well), and the update that JUST came out fixes it.好吧,在研究了整个堆栈、github、msdn 等之后,我发现这些帖子让我看到这是 VS 的 16.6.2 版本中的一个错误(也可能是早期版本),以及刚刚发布的更新修复它。

Related links to the issue问题的相关链接

ASP.Net Core MVC needs <environmentVariables> in web.config to work ASP.Net Core MVC 需要 web.config 中的 <environmentVariables> 才能工作

which led to: https://github.com/dotnet/websdk/issues/564#issuecomment-644714341这导致: https://github.com/dotnet/websdk/issues/564#issuecomment-644714341

which led to this comment about the VS update: https://github.com/dotnet/websdk/issues/1510#issuecomment-652012087这导致了关于 VS 更新的评论: https://github.com/dotnet/websdk/issues/1510#issuecomment-652012087 在此处输入图像描述

The docs linked in that comment don't specifically mention this fix.该评论中链接的文档没有具体提及此修复程序。 I couldn't find the full release notes.我找不到完整的发行说明。 https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes#16.6.3 https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes#16.6.3

But ANYWAYS, updating to VS 2019 16.6.3 worked.但无论如何,更新到 VS 2019 16.6.3 有效。 I can keep my aspNetCore web.config settings, and launch profile environment variables now load correctly.我可以保留我的 aspNetCore web.config 设置,并且现在可以正确加载启动配置文件环境变量。 Huzzah!嘘!

Talk about perfect timing for an update...谈论更新的最佳时机......

Also, many browser tabs and RAM bytes lost their lives in search for this answer.此外,许多浏览器选项卡和 RAM 字节在寻找这个答案时失去了生命。 Respect.尊重。 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core 2.2 如何在项目之间共享 web.config/launchSettings.json 文件 - ASP.NET Core 2.2 How to share web.config/launchSettings.json files among projects 为什么 Azure 应用服务不使用 launchSettings.json 配置? - Why Azure App Service doesn;t use launchSettings.json config? launchSettings.json 和 appSettings.json 是如何工作的? - how do launchSettings.json and appSettings.json work? 使用自定义部分时,为什么必须在我的app.config中指定程序集而不是为web.config指定程序集 - Why do I have to specify the assembly in my app.config but not for my web.config when using custom sections 从Mac上的命令行运行ASP.NET 5应用程序时是使用launchSettings.json吗? - Is launchSettings.json used when running ASP.NET 5 apps from the command line on Mac? 运行单元测试时是否可以加载来自 launchSettings.json 文件的配置文件? - Can a profile from the launchSettings.json file be loaded when running unit tests? 来自 launchsettings.json 的环境变量正在测试中使用吗? - Environment variables from launchsettings.json are being used in tests? Visual Studio在发布过程中使用launchSettings.json配置文件中定义的哪个? - Which of defined in launchSettings.json profiles Visual Studio uses during publication? 如何从 launchsettings.json 获取当前应用程序 url - How to get current application url from launchsettings.json 为什么 Webservice 不使用来自 web.config 的变量 AS.NETCORE_ENVIRONMENT - Why Webservice doesn't use variable ASPNETCORE_ENVIRONMENT from web.config
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM