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
- Create a new ASP.NET Core Web Application, 3.1
- Create a launchSettings.json profile that uses IISExpress, and has
"environmentVariables": { "ASPNETCORE_ENVIRONMENT": "SomeTestValue" }
- Run that launch profile and check the environment value on
IWebHostEnvironment
in Configure
of Startup.cs.
- Note that the
env.EnvironmentName
should match "SomeTestValue".
- 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>
- 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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…