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 issue
Launched 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
To add on @CodeCaster answer why you can't actually retrieve it and what happens behind the scenes. Yes you can use IWebHostEnvironment
to retrieve what you need but the enviroment name IS NOT an enviroment variable that asp.net core sets it is something it simply reads from if you dont have it in your lauchsettings.json
There are 3 stages of enviroment:
Now the difference here is that Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
looks always at the process level it does not look at the OS level. You can of course change that by using GetEnvironmentVariable(String, EnvironmentVariableTarget)
but that wont help you as @CodeCaster mentioned. So use the IWebHostEnvironment
to get the enviroment name should be sufficient for your case but you may have set the DOTNET_ENVIROMENT in machine level or something so make sure to know what you specifically need.
But how can
IWebHostEnviroment
retrieve it?
Its because those "enviroment names" are not enviroment variables. ASP.Net Core does set any kind of enviroment variable. It insteads defines it as a way for you to say ok this is a development enviroment and this is a production one. It can read from the Machine or User Enviroment variables but it does not create any varaibles of some sort.
So the reason why you cant read those enviroment variables while using IIS is because those enviroment variables dont exist in the first place.
Also furthermore the IWebHostEnvironment
does not somehow magically read the enviroment variables. They simply load it from the lauchsettings.json
so again there is no enviroment variable setting.
More specifically in the source code:
hostingEnvironment.EnvironmentName =
options.Environment ??
hostingEnvironment.EnvironmentName;
It is either retrieved by the options you have defined and if you have not defined it then it will stay the same.
Furthermore process enviroment variables do not persist after terminating the process so there is no need for machine restart or clean up of those variables. (General comment)