根目錄創建 hosting.json
{ "urls": "http://*:8081" }
Program.cs
public static void Main(string[] args) { // 生成承載 web 應用程序的 Microsoft.AspNetCore.Hosting.IWebHost。Build是WebHostBuilder最終的目的,將返回一個構造的WebHost,最終生成宿主。 var host = CreateHostBuilder(args).Build(); // 運行 web 應用程序並阻止調用線程, 直到主機關閉。 // 創建完 WebHost 之后,便調用它的 Run 方法,而 Run 方法會去調用 WebHost 的 StartAsync 方法 // 將Initialize方法創建的Application管道傳入以供處理消息 // 執行HostedServiceExecutor.StartAsync方法 // ※※※※ 有異常,查看 Log 文件夾下的異常日志 ※※※※ host.Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) //<--NOTE THIS .ConfigureWebHostDefaults(webBuilder => { webBuilder .ConfigureKestrel(serverOptions => { serverOptions.AllowSynchronousIO = true;//啟用同步 IO })
//方式一 .ConfigureAppConfiguration(builder => { builder.AddJsonFile("hosting.json", optional: true); }) .UseStartup<Startup>()
//方式二 //.UseUrls("http://*:8081") });