Configuring the hosting environment for an ASP. NET Core site with "Hosting.json"

Source: Internet
Author: User
Tags hosting

Typically, we use hard-coded methods in Prgram.cs to configure the Hosting environment for an ASP. NET Core site, which is most commonly used. Useurls ().

 public  class   program{ static  void  Main (string  [] (args) { var  host = new   Webhostbuilder () . Useurls (    " http://* :  style= "color: #800000;" "   ) . Usekestrel (). Usecontentroot (Directory.GetCurrentDirectory ()). Usestartup  <startup> ().        Build (); Host.    Run (); }}

But this hard-coded way to bind ports can cause trouble deploying multiple sites on the same Linux server because different sites need to bind different ports. Unless you have agreed on the ports used by each project at development time, it is easy to run into port conflict issues during deployment, forcing you to modify the code.

If you can set the bound port through the configuration file, this problem will be solved. Does ASP. NET Core provide a corresponding solution? With this problem, check out the source of aspnet/hosting today, in the samplestartups StartupFullControl.cs found the answer:

var New Configurationbuilder ()    . Addcommandline (args)    "aspnetcore_")    . Addjsonfile ("hosting.json"true)    . Build (); var New Webhostbuilder ()    . Useconfiguration (config)

The original can be configured through Hosting.json, the following actual experience.

First create a Hosting.json file:

{  "server.urls": "http://*:5000;http://*:8001",  "Environment": "Development"}

In addition to configuring Server.urls in the above configuration, the environment (default is production) is also configured.

Then use the configuration in the Program.cs in the Hosting.json:

 Public classprogram{ Public Static voidMain (string[] args) {        varConfig =NewConfigurationbuilder (). Addjsonfile ("Hosting.json", Optional:true)            .        Build (); varHost =NewWebhostbuilder () . Useurls (  "http://*:5000" )            . Usekestrel (). Usecontentroot (Directory.GetCurrentDirectory ()). Usestartup<Startup>()            . Useconfiguration (config) .        Build (); Host.    Run (); }}

Attention must be put on the above. Useurls () is removed, otherwise the configuration in the Hosting.json will be overwritten by it.

Also note that in Project.json, in addition to adding "Hosting.json" in "Publishoptions", also add "Copytooutput" in "Buildoptions", "Hosting.json" ", otherwise the Hosting.json file will not be found in the Bin folder at run time.

"Buildoptions": {  "Emitentrypoint": True,  "Preservecompilationcontext": True,  "copytooutput": " Hosting.json "}," Publishoptions ": {  " include ": [    " Hosting.json "  ]}

Finally, run the site with the dotnet Run command to experience the actual effect.

Hosting environment:developmentcontent root Path:c:\dev\cnblogs.webdemonow listening On:http://*:5000now listening on : Http://*:8001application started. Press CTRL + C to shut down.

Configuring the hosting environment for an ASP. NET Core site with "Hosting.json"

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.