Two days ago, I reprinted a .NET core startup analysis, because I was entangled in the default 5000 port when it was released, so I studied it carefully.
1. IIS integration
If you use IIS as a host, then these are not a problem, powerful IIS can help us configure the domain name, port, etc. of the site. As for how to deploy a asp.net core web application on IIS, that's not the point here. Roughly describe it:
You need to download the Net Core SDK and Server Hosting, and the download address is https://www.microsoft.com/net/download
After installation, check whether the .NET Core SDK is installed successfully on the command line dotnet info
Whether the server host is successfully installed in the IIS module and handler mapping is shown below
Then set up the site and specify the files to the publishing site
Finally, the program pool should be configured, and choose unmanaged, so that there is a server host forwarding request.
2. Linux environment
Not to mention the specific installation, there are also a lot of them. According to the instructions on the official website, that is, install the .NET Core running environment and it can be running.
Here is a recommended blog post for your own reference: Deploying ASP.NET Core applications to production (CentOS7)
Back to the main point, how to configure URL and port parameters
1. Specify in the Main method of Program
This approach is inflexible and not so elegant even if it is read by adding a configuration file. At this time, I felt that Microsoft would definitely not recommend such a use, so I continued to look for it.
2. Pass the environment variable
I saw an article on the Internet How to configure Kestrel URLs in ASP.NET Core RC2,
Although it is still configured through the configuration file, it does not need to read out the configuration information to other articles, and it can be used directly by binding, or paste the code to see:
hosting.json
Program.cs
This way it can also listen
Now listening on: http://localhost:60000
Now listening on: http://localhost:60001
Isn't it amazing! I can't stand the actual combat, deduct the source code! By far the best thing about .NET Core is that it has source code!
By sourcing, we can know that it is mainly the WebHostBuilder class, under the Microsoft.AspNetCore.Hosting namespace.
The main method is Build
The main focus here is to build a WebHost object and then look further
By looking at the source code through the Initialize method, we can know the URL address created by the EnsureServer method
Here we can see that it reads _config[WebHostDefaults.ServerUrlsKey] and _config[DeprecatedServerUrlsKey] from the configuration.
The value of WebHostDefaults.ServerUrlsKey is a fixed value
The value of DeprecatedServerUrlsKey is defined at the beginning of the WebHost object
Oh! The truth is revealed. So we can set "server.urls" in the configuration file.
Summary:
To sum up, asp.net core will read the configuration in the environment variable when it starts, and the actual point is to add the following configuration to the project properties:
It has been started in console mode and found that the port has been switched.
So this is a development environment, how to deploy it on the production line? This is also very simple, taking Linux deployment as an example, using the daemon supervisor to start the program, add environment variables to the supervisor's startup configuration:
Succeed with flying colors! Not a single line of code needs to be changed, haha~ |