前兩天轉載一篇.net core 啓動分析,因爲發佈時候一直糾結在默認5000端口上,因此好好研究了一下。html
若是經過IIS當宿主的話,那這些都不是事情,強大的IIS能夠幫助咱們對站點的域名、端口等等等等的配置。至於如何在IIS上部署asp.net core的web應用,就不是這裏的重點。大體簡單的描述一下:linux
須要下載Net Core SDK 與 Server Hosting,下載地址https://www.microsoft.com/net/downloadweb
安裝完查看.net core sdk是否安裝成功命令行dotnet info json
server host 是否安裝成功iis模塊與處理程序映射中查看以下app
而後創建站點,指定到發佈站點的文件asp.net
最後就是應該程序池配置,選擇無託管,這樣有server host轉發請求。ide
具體安裝就不說了,也是一大堆。根據官網指示,也就是安裝.net core運行環境就能夠運行了。post
這裏推薦一篇博文,你們自行參考 將ASP.NET Core應用程序部署至生產環境中(CentOS7)ui
回到重點,如何配置url及端口參數url
1.在Program的Main方法裏面指定
public static void Main(string[] args) { var host = new WebHostBuilder() .UseUrls("http://localhost:5001") .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run(); }
這種方法不靈活,即便經過增長配置文件去讀,也不是那麼優雅。這個時候,本人就以爲微軟確定不會推薦這麼用的,因而繼續找。
2.經過環境變量
網上看到有一篇How to configure Kestrel URLs in ASP.NET Core RC2,
雖然仍是經過配置文件配置,可是它不向其餘文章,不須要讀出配置信息,直接綁定就能用,仍是貼代碼看:
hosting.json
{ "server.urls": "http://localhost:60000;http://localhost:60001" }
Program.cs
public static void Main(string[] args) { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("hosting.json", optional: true) .Build(); var host = new WebHostBuilder() .UseKestrel() .UseConfiguration(config) .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run(); }
這樣它也能監聽
Now listening on: http://localhost:60000
Now listening on: http://localhost:60001
是否是很神奇!實戰受不了了,扣源碼!目前爲止.net core最好的地方就是有源碼!
經過溯源,咱們能夠知道主要是 WebHostBuilder 這個類,在Microsoft.AspNetCore.Hosting命名空間下。
主要的方法仍是Build
/// <summary> /// Builds the required services and an <see cref="IWebHost"/> which hosts a web application. /// </summary> public IWebHost Build() { // Warn about deprecated environment variables if (Environment.GetEnvironmentVariable("Hosting:Environment") != null) { Console.WriteLine("The environment variable 'Hosting:Environment' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'"); } if (Environment.GetEnvironmentVariable("ASPNET_ENV") != null) { Console.WriteLine("The environment variable 'ASPNET_ENV' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'"); } if (Environment.GetEnvironmentVariable("ASPNETCORE_SERVER.URLS") != null) { Console.WriteLine("The environment variable 'ASPNETCORE_SERVER.URLS' is obsolete and has been replaced with 'ASPNETCORE_URLS'"); } var hostingServices = BuildHostingServices(); var hostingContainer = hostingServices.BuildServiceProvider(); var host = new WebHost(hostingServices, hostingContainer, _options, _config); host.Initialize(); return host; }
這邊主要是構建一個WebHost對象,而後更進去看
經過Initialize方法查看源代碼,咱們能夠知道是EnsureServer這個方法建立的url地址
private void EnsureServer() { if (Server == null) { Server = _applicationServices.GetRequiredService<IServer>(); var addresses = Server.Features?.Get<IServerAddressesFeature>()?.Addresses; if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0) { var urls = _config[WebHostDefaults.ServerUrlsKey] ?? _config[DeprecatedServerUrlsKey]; if (!string.IsNullOrEmpty(urls)) { foreach (var value in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { addresses.Add(value); } } if (addresses.Count == 0) { // Provide a default address if there aren't any configured. addresses.Add("http://localhost:5000"); } } } }
這裏咱們能夠知道,原來它本身會從配置裏面去讀 _config[WebHostDefaults.ServerUrlsKey] 和 _config[DeprecatedServerUrlsKey]
WebHostDefaults.ServerUrlsKey的值是固定值
DeprecatedServerUrlsKey的值在WebHost這個對象一開始就定義了
哦!真相大白了。因此咱們在配置文件裏面設置「server.urls」就能夠了。
總結:
綜上所述,asp.net core啓動的時候會自行讀取環境變量裏面的配置,實際點就是在項目屬性裏面增長以下配置:
已控制檯方式啓動,發現已經切換了端口。
那麼這個是在開發環境,如何在產線部署呢。這個也很簡單,以linux上部署爲例,以守護進程supervisor啓動程序,在supervisor的啓動配置裏面增長環境變量:
environment=ASPNETCORE_URLS='http://*:5001'
大功告成!一行代碼都不須要改,哈哈~
參考文獻: