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

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

问题描述

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

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.

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

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.

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

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.

现在更加好奇的是,如果我仅从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:

这可能与主机模型从OutOfProcess(默认为.NET 2.1)更改为InProcess(默认为.NET 3.1)有关.实际上,如果我们将项目的hostingModel强制设置为OutofProcess,启动配置文件也可以正常运行,但我们宁愿保留InProcess.

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.

那么这是怎么回事?我们缺少一些迁移步骤吗?我们遵循了MSDN上的所有步骤,从此处开始,一直到3.1:

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

  1. 创建一个新的ASP.NET Core Web应用程序3.1
  2. 创建使用IISExpress并具有"environmentVariables": { "ASPNETCORE_ENVIRONMENT": "SomeTestValue" }
  3. 的launchSettings.json配置文件
  4. 运行该启动配置文件,并检查Startup.cs ConfigureIWebHostEnvironment上的环境值.
  5. 请注意,env.EnvironmentName应该与"SomeTestValue"匹配.
  6. 现在将以下web.config文件添加到您的项目中:
  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. 运行相同的配置文件并检查环境值.它应该是您计算机的默认值(在大多数情况下为生产",当未找到/未指定时为默认值).

推荐答案

FIX:将Visual Studio 2019更新到v16.6.3(提前1天发布!)

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

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.

> ASP.Net Core MVC需要< ; environmentVariables>在web.config中正常工作

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

这引起了有关VS更新的评论: https://github .com/dotnet/websdk/issues/1510#issuecomment-652012087

which led to this comment about the VS update: https://github.com/dotnet/websdk/issues/1510#issuecomment-652012087

该评论中链接的文档未具体提及此修复程序.我找不到完整的发行说明. https://docs.microsoft.com/zh-cn/visualstudio/releases/2019/release-notes#16.6.3

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

但是无论如何,更新到VS 2019 16.6.3还是可以的.我可以保留我的aspNetCore web.config设置,并且现在可以正确加载启动配置文件环境变量.众议院!

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...

此外,许多浏览器选项卡和RAM字节在寻找此答案时丧命.尊重.

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天全站免登陆