Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dotnet core ignores ASPNETCORE_ENVIRONMENT variable

I created a simple webapi project with dotnet core.

I'm trying to set the running environment using the ASPNETCORE_ENVIRONMENT system variable.

C:\devel\apps\webapi>set ASPNETCORE_ENVIRONMENT=Production

C:\devel\apps\webapi>echo %ASPNETCORE_ENVIRONMENT%
Production

C:\devel\apps\webapi>dotnet run
Using launch settings from C:\devel\apps\webapi\Properties\launchSettings.json...
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\SSCARANO\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
Hosting environment: Development
Content root path: C:\devel\apps\webapi
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

I tried opening a different console, and also setting it logged in as administrator, but the app is always running on Development mode.

I saw this (https://stackoverflow.com/a/40102470/47633) related question, but I'm not using IIS

like image 659
opensas Avatar asked Aug 14 '18 23:08

opensas


2 Answers

I suspect that the issue you're encountering is that the ASPNETCORE_ENVIRONMENT variable is defined in the C:\devel\apps\webapi\Properties\launchSettings.json file.

The way dotnet run works is it loads this file and finds the first profile which commandName is project, and uses it to run the application. There's a high chance, if you haven't made any changes to that file, that the environmentVariables section of that profile contains the ASPNETCORE_ENVIRONMENT variable set to Development.

You have 2 options if you want to run the app in Production mode:

  • change the existing profile and set the variable to Production, meaning every time you execute dotnet run, it's going to run the app in production mode; or
  • add a new profile where the ASPNETCORE_ENVIRONMENT variable is set to Production. You can then use it if you execute dotnet run --launch-profile <name-of-your-new-profile>
like image 184
Mickaël Derriey Avatar answered Sep 19 '22 20:09

Mickaël Derriey


The problem is how you have set the environment variable. On the command line you don't need the quotes, so you should instead have this:

C:\> set ASPNETCORE_ENVIRONMENT=Production

C:\> echo %ASPNETCORE_ENVIRONMENT%
Production
like image 29
DavidG Avatar answered Sep 22 '22 20:09

DavidG