.Net Core 修改默認的啟動端口
今天無意中發現一個變化,因為很久沒看.net core的項目了,發現項目啟動的默認端口已經不是5000了,記得很清楚,最早那還是.net core 1.x版本的時候,每次啟動都會默認是5000端口號,而現在不是了。借此機會在來說一下,關於.net core項目修改默認端口號的解決方案,我們最熟知的是一種解決方案就是直接在Program.cs中創建WebHost對象的時候,使用UseUrls()方法,配置要使用的端口,如下所示:
這是我們再熟悉不過的修改方法了,啟動以後,5001和5002端口都會開啟監聽,所以我們通過這兩個地址都會訪問到我們的網站。但是這種方式的缺點就是我們要修改代碼,重新編譯運行,這並不是我們想要的最好的解決方案,如果可以直接在配置文件中配置就完美了,便在一個個人博客裏看到了一個方法,地址:http://benfoster.io/blog/how-to-configure-kestrel-urls-in-aspnet-core-rc2 添加一個json文件,叫做hosting.json,意為服務配置,當然起什麽名字不重要,裏面只需要添加一個節點即可,內容如下:
{ "server.urls": "http://localhost:5001;http://localhost:5002" }
添加了一個server.urls節點,然後再修改Program.cs文件,
運行後,顯示如下:
5001和5002端口都開啟了監聽。
這不禁引起了我的興趣,便翻起了關於這一塊兒的源碼。關於Hosting的源碼地址:https://github.com/aspnet/Hosting
下面我們層層深入,究其原因,查看IWebHost接口的Build方法:
/// <summary> /// Builds the required services and an <see cref="IWebHost"/> which hosts a web application. /// </summary> public IWebHost Build() { if (_webHostBuilt) { throw new InvalidOperationException(Resources.WebHostBuilder_SingleInstance); } _webHostBuilt = true; var hostingServices = BuildCommonServices(out var hostingStartupErrors); var applicationServices = hostingServices.Clone(); var hostingServiceProvider = GetProviderFromFactory(hostingServices); if (!_options.SuppressStatusMessages) { // 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 logger = hostingServiceProvider.GetRequiredService<ILogger<WebHost>>(); // Warn about duplicate HostingStartupAssemblies foreach (var assemblyName in _options.GetFinalHostingStartupAssemblies().GroupBy(a => a, StringComparer.OrdinalIgnoreCase).Where(g => g.Count() > 1)) { logger.LogWarning($"The assembly {assemblyName} was specified multiple times. Hosting startup assemblies should only be specified once."); } AddApplicationServices(applicationServices, hostingServiceProvider); var host = new WebHost( applicationServices, hostingServiceProvider, _options, _config, hostingStartupErrors); try { host.Initialize(); return host; } catch { // Dispose the host if there‘s a failure to initialize, this should clean up // will dispose services that were constructed until the exception was thrown host.Dispose(); throw; }
在Build方法裏主要是初始化了一個WebHost對象,再進入WebHost的源代碼,
private void EnsureServer() { if (Server == null) { Server = _applicationServices.GetRequiredService<IServer>(); var serverAddressesFeature = Server.Features?.Get<IServerAddressesFeature>(); var addresses = serverAddressesFeature?.Addresses; if (addresses != null && !addresses.IsReadOnly && addresses.Count == 0) { var urls = _config[WebHostDefaults.ServerUrlsKey] ?? _config[DeprecatedServerUrlsKey]; if (!string.IsNullOrEmpty(urls)) { serverAddressesFeature.PreferHostingUrls = WebHostUtilities.ParseBool(_config, WebHostDefaults.PreferHostingUrlsKey); foreach (var value in urls.Split(new[] { ‘;‘ }, StringSplitOptions.RemoveEmptyEntries)) { addresses.Add(value); } } } } }
不難找到是在上述方法裏面,添加的URL地址,在這行代碼中:
var urls = _config[WebHostDefaults.ServerUrlsKey] ?? _config[DeprecatedServerUrlsKey];
W這裏的URL判斷,如果當前默認的配置為空則使用後面的配置,而DepreacatedServerUrlsKey在一開始就已經定義了,
private static readonly string DeprecatedServerUrlsKey = "server.urls";
該參數的默認值為server.urls,所以我們在hosting.json中直接配置該鍵值對,.net core啟動後會自動讀取配置文件,使用配置的端口。其他相關配置,大家就自行搜索吧。
.Net Core 修改默認的啟動端口