Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dotnet run OR dotnet watch with development environment from command line?

I am using dotnet watch command to run asp.net core project. However, by default, it is picking up the Production as an environment.

I have tried both options using:

1) > dotnet watch ASPNETCORE_ENVIRONMENT=Development  2) > dotnet run ASPNETCORE_ENVIRONMENT=Development 

But it still picks up production as an environment.

Note: In visual studio environment variable is set in project properties as Development by default and running from visual studio picks that variable.

Question is: How to run dotnet core project in development from command line using either?:

1) dotnet run 2) dotnet watch 
like image 279
Nexus23 Avatar asked May 19 '16 11:05

Nexus23


People also ask

How do I run a .NET core command line?

All the commands start with driver named dotnet. The driver starts the execution of the specified command. After dotnet, we can supply command (also known as verb) to perform a specific action. Each command can be followed by arguments and options.

What is dotnet Run command?

The dotnet run command provides a convenient option to run your application from the source code with one command. It's useful for fast iterative development from the command line. The command depends on the dotnet build command to build the code.


2 Answers

ASPNETCORE_ENVIRONMENT is an environment variable (and AFAIK) not a switch to the dotnet cli.

So what you would do is set it prior to using the tool:

rem Windows C:\> set ASPNETCORE_ENVIRONMENT=Development C:\> dotnet ...  rem Unix $ export ASPNETCORE_ENVIRONMENT=Development $ dotnet ... 
like image 50
Christian.K Avatar answered Sep 24 '22 06:09

Christian.K


You don't have to use environment variables if you adjust how the WebHostBuilder processes its configuration. This is merely the default for dotnet new -t web. For example, if you wanted to be able to set the default environment to "development" instead of production and facilitate overriding the environment in the command line, you could do that by modifying the normal Program.cs code from this ...

    public static void Main(string[] args) {         var host = new WebHostBuilder()             .UseKestrel()             .UseUrls("http://0.0.0.0:5000")             .UseContentRoot(Directory.GetCurrentDirectory())             .UseIISIntegration()             .UseStartup<Startup>()             .Build();          host.Run();     } 

... into something like this ...

    private static readonly Dictionary<string, string> defaults =         new Dictionary<string, string> {             { WebHostDefaults.EnvironmentKey, "development" }         };      public static void Main(string[] args) {         var configuration =             new ConfigurationBuilder()                 .AddInMemoryCollection(defaults)                 .AddEnvironmentVariables("ASPNETCORE_")                 .AddCommandLine(args)                 .Build();          var host =             new WebHostBuilder()                 .UseConfiguration(configuration)                 .UseKestrel()                 .UseUrls("http://0.0.0.0:5000")                 .UseContentRoot(Directory.GetCurrentDirectory())                 .UseIISIntegration()                 .UseStartup<Startup>()                 .Build();          host.Run();     } 

Doing this, the environment variables would still work, but you can override it on the command line without any third-party dependencies, like so:

dotnet run environment=development dotnet run environment=staging 

This is actually what the yeoman generators do.

like image 30
Technetium Avatar answered Sep 23 '22 06:09

Technetium