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

那年仲夏 提交于 2019-12-03 01:09:43

问题


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

回答1:


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 ...



回答2:


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.




回答3:


You can also set the variable inline when calling dotnet:

ASPNETCORE_ENVIRONMENT=Development dotnet run

I have found this is great for NPM scripts, but must always be called right before dotnet, e.g.:

{
  ...
  "scripts": {
    "start": "cd MyApp && ASPNETCORE_ENVIRONMENT=Development dotnet run",
    "watch": "cd MyApp && ASPNETCORE_ENVIRONMENT=Development dotnet watch"
  },
}

Note: This only works in OS X or Linux; for a cross-platform solution, you can use cross-env:

npm install cross-env -D

Then change the scripts to:

{
  ...
  "scripts": {
    "start": "cd MyApp && cross-env ASPNETCORE_ENVIRONMENT=Development dotnet run",
    "watch": "cd MyApp && cross-env ASPNETCORE_ENVIRONMENT=Development dotnet watch"
  },
}



回答4:


Source: https://blogs.msdn.microsoft.com/webdev/2017/02/14/building-single-page-applications-on-asp-net-core-with-javascriptservices/

Styling added.

If you’re using PowerShell in Windows, execute $Env:ASPNETCORE_ENVIRONMENT = "Development"

If you’re using cmd.exe in Windows, execute setx ASPNETCORE_ENVIRONMENT "Development", and then restart your command prompt to make the change take effect

If you’re using Mac/Linux, execute export ASPNETCORE_ENVIRONMENT=Development




回答5:


Check documentation

https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-run?tabs=netcore21

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1

dotnet run --launch-profile EnvironmentsSample

launchSettings.json

{ 
  "profiles": {    
    "EnvironmentsSample": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      },
      "applicationUrl": "http://localhost:54340/"
    },   
  }
}



回答6:


With credit to @Technetium 's answer, here's an update to dotnetcore 2.0 that works really well for me:

public class Program
    {
        private static readonly Dictionary<string, string> defaults = new Dictionary<string, string> {{ WebHostDefaults.EnvironmentKey, "Development" }                                                                      };
        private static IConfiguration config;
        public static void Main(string[] args)
        {
            config = new ConfigurationBuilder()
                .AddInMemoryCollection(defaults)
                .AddEnvironmentVariables("ASPNETCORE_")
                .AddCommandLine(args)
                .Build();
            BuildWebHost(args).Run();
        }
public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseConfiguration(config)
                /* Following setting already accounted for in CreateDefaultBuilder() : https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.webhost.createdefaultbuilder?view=aspnetcore-2.0 */
                // .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>()
                .Build();
    }

Thanks again, @Technetium



来源:https://stackoverflow.com/questions/37322565/dotnet-run-or-dotnet-watch-with-development-environment-from-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!