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. IWebHostBuilder
Configuration is added to the app's configuration, but the converse is not true. ConfigureAppConfiguration
It does not affect the IWebHostBuilder
configuration.
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 IConfiguration
the host to copy constructor key configuration. Therefore, JSON, INI, and XML settings file settings reloadOnChange: true
do not have any effect.