10

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

1
  • I also have the same behavior. Running .NET 8 on Ubuntu 22. Commented Jun 19 at 19:14

2 Answers 2

11

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

I copy pasted your answer but it stills runs on Development mode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.