I have stumbled across a strange situation for an ASP.NET Core 3.1 application: environmentVariables defined in launchSettings.json are not set (e.g. Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
is null
).
The launchsettings.json looks like this:
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60907",
"sslPort": 44364
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger/index.html",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"MyApp.WebApi": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger/index.html",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
and I am using IIS Express
.
Things I have investigated or tried:
Pulled the project on another machine and it works as expected:
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"
is"Development"
.Ran a very similar project (virtually the same configuration) on the same machine and it works as expected
Removed all launchsettings.json content except for
IIS Express
and it still has the same issueLaunched using another profile and it works as expected
Closed Visual Studio -> removed .vs folder -> reopened VS + rerun project. The issue persists
Restarted the machine, the problem persists
Added other environment variables in the profile and they are also ignored
Compared the working projects vs. the non-working project IISExpress applicationhost.config files and did not notice any difference (except for the project paths).
The only workaround I have right now is to make a local change in Program.cs and make sure I never commit it, but I am looking for a real fix:
string originalEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", originalEnv ?? "Development");
I have run out of ideas to try. I assume it is something related to an IIS Express configuration on that particular machine for this particular application, but I could not find anything related to environment variables in %userprofile%\My Documents\IISExpress\config\applicationhost.config