NET Web host (Web Host) [next]

Reload configuration

       Use Configuration to configure a Web host. In the following example, the host configured in an alternative form hostsettings.json specified file. From hostsettings.json any configuration files are loaded command-line parameters may be rewritten. Built-in configuration (config files) are UseConfiguration used to configure the host. IWebHostBuilderConfiguration is added to the app's configuration, but the converse is not true. ConfigureAppConfigurationIt does not affect the IWebHostBuilderconfiguration.

      First we overloaded by theUseUrls 以hostsettings.json 文件形式提供的配置,然后是命令行参数:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("hostsettings.json", optional: true)
            .AddCommandLine(args)
            .Build();

        return WebHost.CreateDefaultBuilder(args)
            .UseUrls("http://*:5000")
            .UseConfiguration(config)
            .Configure(app =>
            {
                app.Run(context => 
                    context.Response.WriteAsync("Hello, World!"));
            });
    }
}

        hostsettings.json

{
    urls: "http://*:5005"
}

       Note: UseConfiguration only supplied from IConfigurationthe host to copy constructor key configuration. Therefore, JSON, INI, and XML settings file settings reloadOnChange: truedo not have any effect.

Guess you like

Origin www.cnblogs.com/qianxingmu/p/12455008.html