Dotnet core ignores ASPNETCORE_ENVIRONMENT variable
Asked Answered
O

2

9

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://mcmap.net/q/479715/-asp-net-core-hosting-environment-variable-ignored) related question, but I'm not using IIS

Octangle answered 14/8, 2018 at 23:21 Comment(0)
E
10

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>
Evolve answered 15/8, 2018 at 1:3 Comment(0)
E
2

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
Eustacia answered 14/8, 2018 at 23:42 Comment(1)
I copy pasted your answer but it stills runs on Development modeOctangle

© 2022 - 2024 — McMap. All rights reserved.