2

From command line, if I run set ASPNETCORE_ENVIRONMENT=Development && dotnet watch run, my Hosting environment is set to Development.

However, if I add the same line as a command to my project.json file, the hosting environment for the watcher is always production:

"commands": {
  "watch": "set ASPNETCORE_ENVIRONMENT=Development && dotnet watch run"
},

Are there any arguments I can pass to dotnet run to make the hosting environment Development? I do need this to work as a command.

Tyson Nero
  • 2,048
  • 5
  • 26
  • 36
  • 3
    if i remember correctly, the support for commands was removed when moving to dotnet-cli. The replacement for it is the tools section – Tseng Jul 08 '16 at 19:08

2 Answers2

3

You can add the Microsoft.Extensions.Configuration.CommandLine package which reads the configuration from command line:

public class Program
{
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
            .AddEnvironmentVariables()
            .AddCommandLine(args)
            .Build();

        var host = new WebHostBuilder()
            .UseEnvironment(config["ASPNETCORE_ENVIRONMENT"])
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

With dotnet run you could do like below:

dotnet run --ASPNETCORE_ENVIRONMENT Development

You should be able to do something similar with dotnet watch run too.

Kiran
  • 56,921
  • 15
  • 176
  • 161
  • I like what you posted in general, but it unfortunately didn't work for my specific use case. I followed your code example, then updated my command with the --ASPNETCORE_ENVIRONMENT Development argument. No luck. In fact the arg wasn't passed at all and blew up on the UseEnvironment line. – Tyson Nero Jul 16 '16 at 01:39
0

I finally figured this out!

The trick was to right click on the Project, go to Properties then select the Debug tab. Next, under Profile, I selected the name of the command defined in my projects.json: "watch". Once selected, I clicked Add by Environment Variables, and added the Name/Value pair ASPNETCORE_ENVIRONMENT and Development.

enter image description here

What this actually does is is upload the launchSettings.json file under Properties in Solution Explorer. This file could also just be edited manually. The output looks something like this:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:56846/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "LendingTree.CreditCards.Web": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Watcher Development": {
      "commandName": "Watcher Development",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
Tyson Nero
  • 2,048
  • 5
  • 26
  • 36
  • Great article that´s help me a lot: [how-to-set-the-hosting-environment-in-asp-net-core](http://andrewlock.net/how-to-set-the-hosting-environment-in-asp-net-core/) – Richard Lee Oct 06 '16 at 19:25