Developing ASP. NET Core Webapi with visual Studio Code learning Notes (10)--Publishing (Windows)

Source: Internet
Author: User
Tags dotnet net core windows server hosting server hosting xunit


This article will be this series The demo example continues to record the process that ASP. NET core publishes on Windows.

ASP. NET core can operate in two ways on Windows. One is self-hosted and the other is published to IIS managed run.

First part, self-hosted one, dependent. Net Core Environment


Modify the contents of the Project.json file to include the configuration content of the file when publishing


1 {
 2 "version": "1.0.0-*",
 3 "testRunner": "xunit", // Set the test tool to xunit
 4 "buildOptions": {
 5 "debugType": "portable",
 6 "emitEntryPoint": true
 7 },
 8 "dependencies": {
 9 "Microsoft.NETCore.App": {
10 "type": "platform",
11 "version": "1.0.0"
12 },
13 "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
14 "Microsoft.AspNetCore.Mvc": "1.0.0",
15 "Microsoft.Extensions.Logging": "1.0.0",
16 "Microsoft.Extensions.Logging.Console": "1.0.0",
17 "Microsoft.Extensions.Logging.Debug": "1.0.0",
18 "Microsoft.Extensions.Logging.Filter": "1.0.0",
19 "NLog.Extensions.Logging": "1.0.0-rtm-alpha2",
20 "Autofac.Extensions.DependencyInjection": "4.0.0-rc3-309",
21 "Microsoft.Extensions.Configuration": "1.0.0",
22 "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
23 "Microsoft.Extensions.Configuration.Json": "1.0.0",
24 "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
25 "xunit": "2.2.0-beta2-build3300",
26 "dotnet-test-xunit": "2.2.0-preview2-build1029",
27 "moq.netcore": "4.4.0-beta8",
28 "Microsoft.AspNetCore.TestHost": "1.0.0",
29 "Newtonsoft.Json": "9.0.1"
30 },
31 "frameworks": {
32 "netcoreapp1.0": {
33 // Set up a compatible framework
34 "imports": [
35 "dotnet54",
36 "portable-net45+win8"
37 ]
38 }
39 },
40 "publishOptions": {
41 // Set the files to be included when publishing
42 "includeFiles": ["appsettings.json", "nlog.config"]
43 }
44 } 


Open the cmd window, enter the project root directory, enter the command dotnet Publish , the project will be compiled and published





Enter the dotnet xxx.dll command to start the program






Access Path http://localhost:5000/api/users , the page is displayed normally.


Second, with the release of the run-time


When you publish across platforms,. Net core can be configured to specify the target platform, and the corresponding runtime is packaged and published at the time of publishing. This allows the target platform to be deployed without the need to install a. Net core environment.



Modify the Project.json file again to increase the target platform


1 {
 2 "version": "1.0.0-*",
 3 "testRunner": "xunit", // Set the test tool to xunit
 4 "buildOptions": {
 5 "debugType": "portable",
 6 "emitEntryPoint": true
 7 },
 8 "dependencies": {
 9 "Microsoft.NETCore.App": {
10 //"type": "platform", // this needs to be commented out
11 "version": "1.0.0"
12 },
13 "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
14 "Microsoft.AspNetCore.Mvc": "1.0.0",
15 "Microsoft.Extensions.Logging": "1.0.0",
16 "Microsoft.Extensions.Logging.Console": "1.0.0",
17 "Microsoft.Extensions.Logging.Debug": "1.0.0",
18 "Microsoft.Extensions.Logging.Filter": "1.0.0",
19 "NLog.Extensions.Logging": "1.0.0-rtm-alpha2",
20 "Autofac.Extensions.DependencyInjection": "4.0.0-rc3-309",
21 "Microsoft.Extensions.Configuration": "1.0.0",
22 "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
23 "Microsoft.Extensions.Configuration.Json": "1.0.0",
24 "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
25 "xunit": "2.2.0-beta2-build3300",
26 "dotnet-test-xunit": "2.2.0-preview2-build1029",
27 "moq.netcore": "4.4.0-beta8",
28 "Microsoft.AspNetCore.TestHost": "1.0.0",
29 "Newtonsoft.Json": "9.0.1"
30 },
31 "frameworks": {
32 "netcoreapp1.0": {
33 // Set up a compatible framework
34 "imports": [
35 "dotnet54",
36 "portable-net45+win8"
37 ]
38 }
39 },
40 "publishOptions": {
41 // Set the files to be included when publishing
42 "includeFiles": [
43 "appsettings.json",
44 "nlog.config"
45 ]
46 },
47 // Target platform
48 "runtimes": {
49 "win7-x64": {},
50 "win10-x64": {},
51 "ubuntu.14.04-x64": {}
52 }
53 } 


CMD window to run the dotnet Restore command to restore the target platform-related packages. This process takes a long time.



After the restore is complete, execute the dotnet publish command to publish






Enter the corresponding publishing directory and execute the WebApiFrame.exe file to start the project.



If the target platform is not explicitly specified, the. Net core selects the current system platform by default. If you want to specify a target platform, you need to execute the command dotnet publish-r {target platform} . The following shows a release to the Ubuntu environment.








The second part, IIS hosting


First, you install a tool. NET Core Windows Server Hosting. The tool supports IIS as a reverse proxy, directing requests to Kestrel servers.



Introducing the relevant NuGet package


"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0"


Modify Program.cs content


 
 
1 using Microsoft.AspNetCore.Hosting;
 2 
 3 namespace WebApiFrame
 4 {
 5     public class Program
 6     {
 7         public static void Main(string[] args)
 8         {
 9             var host = new WebHostBuilder()
10                 .UseKestrel()
11                 .UseIISIntegration()
12                 .UseStartup<Startup>()
13                 .Build();
14 
15             host.Run();
16         }
17     }
18 }


Add Web. config to the project root and configure it to the list of published include files


 
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <configuration>
 3 
 4   <!--
 5     Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
 6   -->
 7 
 8   <system.webServer>
 9     <handlers>
10       <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
11     </handlers>
12     <aspNetCore processPath="dotnet" arguments=".\WebApiFrame.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
13   </system.webServer>
14 </configuration>


Start configuring IIS after performing dotnet publish Release






Modify the application pool with the. Net CLR version modified to: no managed code






Start the website, access the address http://localhost:8080/api/users , and the page will display the content.



In the example above, IIS provides a mechanism for reverse proxies through the ASP. NET Core Module.



By accessing the IIS address, the request is directed to the Kestrel server built into ASP. NET core, processed and then back to IIS. The whole process of IIS is only as a bridge and does not do any logical processing.





Last Words

This series is over here. The reason why you chose to use the Visual Studio Code tool to develop the ASP. NET Core Web Api is to gradually learn to understand several important features and functions in ASP. The future in the actual development process can better choose the appropriate technical solutions.


Developing ASP. NET Core Webapi with visual Studio Code learning Notes (10)--Publishing (Windows)


Related Article

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.