5

I use below code to add CommandLine in my .Net-Core 1.2 environment, I can use the command line like dotnet run --urls "http://a.cn.com:5000" --environment "Production".

public static void Main(string[] args)
{
     var config = new ConfigurationBuilder()
                .AddJsonFile("hosting.json", optional: true)
                .AddCommandLine(args)
                .Build();

     var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .UseConfiguration(config)
                .Build();

     host.Run();
}

And After migrate to .net core 2.0 it becomes like that

public static void Main(string[] args)
{
    BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args)
{
    return WebHost.CreateDefaultBuilder(args)
                .ConfigureLogging((context, logging) =>
                {
                    logging.AddSerilog();
                })
                .UseStartup<Startup>()
                .Build();
 }

I use the same command line ,it fails. How to add the Command in .net core 2.0 ?

Supply: 2017.08.17 launchsetting.json below :

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:5005/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Robert.Project.Web": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:5006"
    }
  }
}

What I expect the listening is Now listening on http://a.cn.com:5000 What I get is that:

enter image description here

And we should not add any other code in Program.cs file, cause CreateDefaultBuilder is contain addCommandLine. for more detail, you can see WebHost.cs#L177

Supply 2017 0817 17:44 UTC+8

[::] is the IPv6 equivalent of IPv4 0.0.0.0.

8
  • 1
    I use the command dotnet run --urls "http://a.cn.com:5000" it Now listening on: http://localhost:5000
    – doublnt
    Aug 16, 2017 at 10:11
  • so is a.cn.com an alias for localhost on your development machine, or the production server? or do you think it ignores the --urls argument and defaults to localhost? it could well be the first because it got the port right... Aug 16, 2017 at 10:17
  • a.cn.com is an alias for localhost on my development machine.
    – doublnt
    Aug 16, 2017 at 10:19
  • 1
    The error show in the console is The launch profile "(Default)" could not be applied. A usable launch profile could not be located.
    – doublnt
    Aug 16, 2017 at 10:40
  • is there a launchsettings.json file? I think that's what it will be looking for by default Aug 16, 2017 at 11:00

1 Answer 1

4

Use UseConfiguration directly to set url address populated by args:

public static IWebHost BuildWebHost(string[] args)
{
        var config = new ConfigurationBuilder().AddCommandLine(args).Build();

        return WebHost.CreateDefaultBuilder(args)
            .UseConfiguration(config)
            .UseStartup<Startup>()
            .Build();
}

And we should not add any other code in Program.cs file, cause CreateDefaultBuilder is contain addCommandLine. for more detail, you can see WebHost.cs#L177

This is not exactly true, as linked CreateDefaultBuilder implementation uses ConfigureAppConfiguration method, not UseConfiguration. And there is a difference (from related Github issue):

The ConfigureAppConfiguration was intended to configure the IConfiguration in the application services whereas UseConfiguration is intended to configure the settings of the WebHostBuilder. Since the url address is a setting on the WebHostBuilder only UseConfiguration will work here.

Note that the WebHostBuilder's configuration is added to the application's configuration but the converse is not true; configuring the application's configuration does not affect the WebHostBuilder's configuration.

1
  • I have tried this but command line arguments are getting overridden by launchsettings.json which didn't use to happen
    – daw
    Nov 9, 2017 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.