Reputation: 2280
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://learn.microsoft.com/en-us/aspnet/core/migration/21-to-22?view=aspnetcore-3.1&tabs=visual-studio
"environmentVariables": { "ASPNETCORE_ENVIRONMENT": "SomeTestValue" }
IWebHostEnvironment
in Configure
of Startup.cs.env.EnvironmentName
should match "SomeTestValue".<?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>
Upvotes: 2
Views: 4435
Reputation: 2280
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 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://learn.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.
Upvotes: 4