为什么当 aspNetCore web.config 部分存在时,launchSettings.json 配置文件中的 environmentVariables 不会加载? [英] Why do environmentVariables from launchSettings.json profiles not load when aspNetCore web.config sections exist?

查看:56
本文介绍了为什么当 aspNetCore web.config 部分存在时,launchSettings.json 配置文件中的 environmentVariables 不会加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们最近将 .NET Core 2.1 Web 应用程序升级到 .NET 3.1.从那时起,我们在 launchSettings.json 中的配置文件就无法正常工作.具体而言,未加载环境变量,特别是ASPNETCORE_ENVIRONMENT".在本地调试以针对不同环境时,我们使用它来切换 appSettings.{Environment}.json.

在我的研究中,似乎 web.config 是这个问题的罪魁祸首.对于上下文,我们在项目中保留了一个 web.config,以便我们可以为我们的部署自定义一些内容.有问题的配置文件使用 IISExpress,因此考虑了 web.config.

如果我删除 web.config,我可以切换启动配置文件并且它们生效.但是,当存在 web.config 时,启动配置文件环境变量不会生效.

现在更奇怪的是,如果我从 web.config 中删除这一部分,启动设置中的环境变量将再次开始工作:

这可能与托管模型从 OutOfProcess(默认 .NET 2.1)更改为 InProcess(默认 .NET 3.1)有关.事实上,如果我们将项目的托管模型强制为 OutofProcess,启动配置文件也能正常工作,但如果可能,我们宁愿保留 InProcess.

那么这里发生了什么?我们是否缺少一些迁移步骤?我们遵循了 MSDN 上的所有步骤,从这里开始一直工作到 3.1:

天哪,它是 Visual Studio.我正在运行 VS 2019 16.6.2,我今天刚收到通知更新到 16.6.3,但推迟了.好吧,在研究了整个堆栈、github、msdn 等之后,我发现这些帖子让我看到这是 16.6.2 版本的 VS(也可能是早期版本)中的一个错误,并且刚刚发布的更新修复它.

问题的相关链接

该评论中链接的文档并未具体提及此修复程序.我找不到完整的发行说明.

We recently upgraded a .NET Core 2.1 Web App to .NET 3.1. Ever since, our profiles in launchSettings.json are not properly working. Specifically, the environmentVariables are not loading, with one in particular being "ASPNETCORE_ENVIRONMENT". We use this to switch appSettings.{Environment}.json when debugging locally to target different environments.

In my research, it seems that the web.config is the culprit for this issue. For context, we keep a web.config in our project so we can customize some things for our deployments. The profiles in question use IISExpress, therefore the web.config is taken into consideration.

If I remove the web.config, I can switch launch profiles and they take effect. However, when a web.config is present, the launch profile environment variables do not take effect.

Now even more curious, if I remove just this section from the web.config, the environment variables from launch settings start to work again:

This may be related to the Hosting Model changes going from OutOfProcess (.NET 2.1 default) to InProcess (.NET 3.1 default). 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.

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

Reproduction Steps

  1. Create a new ASP.NET Core Web Application, 3.1
  2. Create a launchSettings.json profile that uses IISExpress, and has "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "SomeTestValue" }
  3. Run that launch profile and check the environment value on IWebHostEnvironment in Configure of Startup.cs.
  4. Note that the env.EnvironmentName should match "SomeTestValue".
  5. Now add the following web.config file to your project:

<?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!)

Holy moley, it was 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. 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.

Related links to the issue

ASP.Net Core MVC needs <environmentVariables> in web.config to work

which led to: 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

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

But ANYWAYS, updating to VS 2019 16.6.3 worked. I can keep my aspNetCore web.config settings, and launch profile environment variables now load correctly. Huzzah!

Talk about perfect timing for an update...

Also, many browser tabs and RAM bytes lost their lives in search for this answer. Respect.

这篇关于为什么当 aspNetCore web.config 部分存在时,launchSettings.json 配置文件中的 environmentVariables 不会加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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