Awesome Awesome - 3 months ago 26
C# Question

Pass hosting environment for self-hosting

I'm trying to run ASP.NET Core (.NET 4.6.1) application as self hosting by means of weblistener

I've add a command:

"commands": {
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
},


and necessary dependencies in
project.json


When I start my application via Visual Studio it's works fine

src\ProjectName\bin\Release\net461\win7-x64\ProjectName.exe


ProjectName.exe starts as web server with
Hosting environment: Development


But when I run it by hand
ProjectName.exe
runs with
Hosting environment: Production
and it's not working properly.
First of all I want find way to run application by hand with
Development environment


In VS I have this option:

enter image description here

Answer

When you start the application Visual Studio will automatically set the environment variables for you (based on settings from launchSettings.json in the properties folder).

When you run from commandline or double-click, you need to set the environment variable yourself using setx ASPNETCORE_ENVIRONMENT "Development" (Commandline) or $Env:ASPNETCORE_ENVIRONMENT = "Development" (Powershell).