.Net Core 修改默认的启动端口

    今天无心中发现一个变化,由于好久没看.net core的项目了,发现项目启动的默认端口已经不是5000了,记得很清楚,最先那仍是.net core 1.x版本的时候,每次启动都会默认是5000端口号,而如今不是了。借此机会在来讲一下,关于.net core项目修改默认端口号的解决方案,咱们最熟知的是一种解决方案就是直接在Program.cs中建立WebHost对象的时候,使用UseUrls()方法,配置要使用的端口,以下所示:git

这是咱们再熟悉不过的修改方法了,启动之后,5001和5002端口都会开启监听,因此咱们经过这两个地址都会访问到咱们的网站。可是这种方式的缺点就是咱们要修改代码,从新编译运行,这并非咱们想要的最好的解决方案,若是能够直接在配置文件中配置就完美了,便在一个我的博客里看到了一个方法,地址:http://benfoster.io/blog/how-to-configure-kestrel-urls-in-aspnet-core-rc2 添加一个json文件,叫作hosting.json,意为服务配置,固然起什么名字不重要,里面只须要添加一个节点便可,内容以下:github

{
  "server.urls": "http://localhost:5001;http://localhost:5002"
}

添加了一个server.urls节点,而后再修改Program.cs文件,web

运行后,显示以下:json

5001和5002端口都开启了监听。app

这不由引发了个人兴趣,便翻起了关于这一起的源码。关于Hosting的源码地址:https://github.com/aspnet/Hostingide

下面咱们层层深刻,究其缘由,查看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在一开始就已经定义了,ui

private static readonly string DeprecatedServerUrlsKey = "server.urls";

该参数的默认值为server.urls,因此咱们在hosting.json中直接配置该键值对,.net core启动后会自动读取配置文件,使用配置的端口。其余相关配置,你们就自行搜索吧。

细心的小伙伴会发如今源代码中,还有两处是关于配置Url的,一处是在上面的Build方法中,有一处判断:

if (Environment.GetEnvironmentVariable("ASPNETCORE_SERVER.URLS") != null)
   {
       Console.WriteLine("The environment variable 'ASPNETCORE_SERVER.URLS' is obsolete and has been replaced with 'ASPNETCORE_URLS'");
   }

若是在环境变量中ASPNETCORE_SERVER.URLS不为空的话,这个值就会替换ASPNETCORE_URLS的值,另外在WebHostBuilder的构造函数中,也有一段判断代码

if (string.IsNullOrEmpty(GetSetting(WebHostDefaults.ServerUrlsKey)))
   {
        // Try adding legacy url key, never remove this.
        UseSetting(WebHostDefaults.ServerUrlsKey, Environment.GetEnvironmentVariable("ASPNETCORE_SERVER.URLS"));
   }

若是获取的默认的配置中urls的值为空,那么就启用ASPNETCORE_SERVER.URLS环境变量中的配置,这也告诉咱们咱们还能够把启动的端口放到环境变量中,右键项目属性,选择调试,

添加环境变量ASPNETCORE_SERVER.URLS配置,以上就是我总结的修改默认启动端口的全部方法了,若是还有其余的方法,欢迎留言。

晚安。

扫描二维码关注个人公众号,共同窗习,共同进步!

相关文章
相关标签/搜索