. Net Core modifies the default boot port

Source: Internet
Author: User
Tags hosting webhost

Today inadvertently found a change, because long time did not see the. NET core project, found that the project started the default port is not 5000, remember very clearly, the first time that is the. NET core 1.x version, each boot will default to 5000 port number, and now is not. Taking this opportunity to take a look at the solution to modify the default port number for a. NET core project, one of the best known solutions is to use the Useurls () method to configure the port to use when creating a Webhost object directly in Program.cs, as follows:

This is what we are familiar with the modification method, after launch, 5001 and 5002 ports will be turned on monitoring, so we will be through these two addresses to our site. But the disadvantage of this way is that we have to modify the code, recompile run, this is not the best solution we want, if it can be configured directly in the configuration file is perfect, in a personal blog saw a method, address: http://benfoster.io/blog/ HOW-TO-CONFIGURE-KESTREL-URLS-IN-ASPNET-CORE-RC2 Add a JSON file, called Hosting.json, meaning the service configuration, of course, what name does not matter, it only need to add a node, The contents are as follows:

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

Add a Server.urls node, and then modify the Program.cs file,

After running, the following is displayed:

The 5001 and 5002 ports all turn on monitoring.

This can not help but arouse my interest, then turned up the source of this together. About Hosting Source Address: https://github.com/aspnet/Hosting

Let's explore the reasons behind these layers, and look at the build method of the Iwebhost interface:

<summary>//Builds the required services and an <see cref= "Iwebhost"/> which hosts a web Applicat        Ion. </summary> public Iwebhost Build () {if (_webhostbuilt) {THR            ow 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 (Envi Ronment. GetEnvironmentVariable ("hosting:environment") = null) {Console.WriteLine ("The Environm                ENT variable ' hosting:environment ' is obsolete and have been replaced with ' aspnetcore_environment '); }               if (environment.getenvironmentvariable ("aspnet_env") = null) {CONSOLE.W                Riteline ("The environment variable ' aspnet_env ' is obsolete and have been replaced with ' aspnetcore_environment '"); } if (Environment.getenvironmentvariable ("Aspnetcore_server"). URLS ") = null) {Console.WriteLine (" The Environment variable ' aspnetcore_server.                URLS ' is obsolete and have 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 assembliesShould 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 U P//would dispose services that were constructed until the exception is thrown host.                Dispose ();            Throw }

The

In the build method primarily initializes a Webhost object and then enters Webhost's source code,

private void Ensureserver () {if (server = = NULL) {Server = _applicationservi Ces.                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[deprecatedser                    Verurlskey]; if (!string. IsNullOrEmpty (URLs)) {serveraddressesfeature.preferhostingurls = Webhostutiliti Es.                        Parsebool (_config, Webhostdefaults.preferhostingurlskey); foreach (var value in URLs. Split (new[] {'; '}, Stringsplitoptions.removeemptyentries)) {addresses .                        ADD (value);  }                    }              }            }        } 

Not hard to find is in the above method, add the URL address, in this line of code:

var urls = _config[webhostdefaults.serverurlskey]?? _config[deprecatedserverurlskey];

The URL here determines that if the current default configuration is empty then the following configuration is used, and Depreacatedserverurlskey has been defined at the outset,

private static readonly string deprecatedserverurlskey = "Server.urls";

The default value for this parameter is Server.urls, so we configure the key-value pair directly in Hosting.json, and the. NET core automatically reads the configuration file when it is started, using the configured port. Other related configuration, you can search for it yourself.

. Net Core modifies the default boot port

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.