WebHostBuilder. How to set url address using CommandLine in .NET-Core 2.0?

后端 未结 1 1753
生来不讨喜
生来不讨喜 2020-12-19 14:33

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 &

相关标签:
1条回答
  • 2020-12-19 14:50

    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.

    0 讨论(0)
提交回复
热议问题