Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

c# - 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. 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:

enter image description here

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).
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

FIX: Update Visual Studio 2019 to v16.6.3 (Released 1 day earlier!)

enter image description here

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 enter image description here

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. enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...