[Asp. Net Core] 1. Asp. Net Core, dotnet watch, and coredotnet in IIS

Source: Internet
Author: User
Tags dotnet hosting

[Asp. Net Core] 1. Asp. Net Core, dotnet watch, and coredotnet in IIS

In the traditional. NET Framework Asp. net Mvc, you can create a site in IIS in the local development environment, you can direct the site directory to the root directory of the asp.net mvc project. After the build, you can refresh the latest changes in the browser, or attach the w3wp process for debugging. However, when developing an Asp. Net Core project based on. Net Core, this method cannot meet our needs:

Asp. Net Core projects must be Pubilsh before they can be deployed to IIS, and an AspNetCoreModule module is required in the middle. This is mainly because the Asp. Net Core project is essentially a Console-type project, and it comes with the Kertrel component to listen for HTTP requests. This makes IIS no longer responsible for running Asp. Net Core, but used as a reverse proxy, as shown in:

So how can we meet the two requirements mentioned above elegantly? The source code of this article is located in (https://github.com/linianhui/aspnetcore/tree/master/dotnet-watch-run)

Dotnet watch

Dotnet watchSome of the functions in dotnet cli tool are used to expand the dotnet cli commands and add a monitoring function for them, that is, when using cli to run a dotnet core project, after you modify the source code of the project, save the source code to refresh the latest changes.. For example, if we use dotnet run to run an Asp. Net Core project, we need to stop the operation and modify the code before we can see the result. If you use dotnet watch run to run the program, you can save the process of stopping the program and directly modify and save it. To enjoy the benefits, you only need to add a reference to your csproj file.

<ItemGroup>    <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" /></ItemGroup> 

When I modify the content of the ValuesController. cs file, watch automatically exits the currently running process and starts it again. Is it convenient?

Dotnet watch in IIS

How to deploy Asp. Net Core to IIS is not explained here. Just paste the Cake deployment script I wrote:

 1 #addin "Cake.IIS" 2 #addin "Cake.Hosts" 3 #addin "Cake.FileHelpers" 4 #addin "Cake.Powershell" 5  6 /// params 7 var target = Argument("target", "default"); 8  9 /// iis web site config10 var webSiteConfig = new {11     host = "api.asp-net-core.dev",12     path = "./src",13     appPoolName = "apppool.noclr"14 };15 16 /// deploy task17 Task("deploy")18     .Does(() =>19 {20     DeleteSite(webSiteConfig.host);21 22     CreateWebsite(new WebsiteSettings()23     {24         Name = webSiteConfig.host,25         Binding = IISBindings.Http.SetHostName(webSiteConfig.host)26                                   .SetIpAddress("*")27                                   .SetPort(80),28         ServerAutoStart = true,29         PhysicalDirectory = webSiteConfig.path,30         ApplicationPool = new ApplicationPoolSettings()31         {32             Name = webSiteConfig.appPoolName,33             IdentityType = IdentityType.LocalSystem,34             MaxProcesses = 1,35             ManagedRuntimeVersion = null36         }37     });38         39     AddHostsRecord("127.0.0.1", webSiteConfig.host);40 });41 42 /// open browser task43 Task("open-browser")44     .Does(() =>45 {46     StartPowershellScript("Start-Process", args =>47     {48         args.Append("chrome.exe")49             .Append("'-incognito'")50             .Append(", '" + webSiteConfig.host + "'");51     });52 });53 54 55 /// default task56 Task("default")57 .IsDependentOn("deploy")58 .IsDependentOn("open-browser");59 60 RunTarget(target);

Because we want to use the dotnet watch command, we did not Build and Publish Asp. Net Core projects during deployment, but directly pointed to its source code directory. So where can I let IIS execute dotnet watch? The answer is in web. config:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3   <system.webServer> 4     

The focus is on the processPath = "dotnet" and arguments = "watch run" of the aspnetcore node ". This configuration node is used by AspNetCoreModule. For detailed configuration parameters, see https://docs.microsoft.com/en-us/aspnet/core/hosting/aspnet-core-module. In this way, AspNetCoreModule will use dotnet watch run to run our project during IIS access.You can edit the code-> Save-> refresh the code in the browser to view the result!.

Use Asp. Net Core attached to the process to debug IIS

Asp. Net Core is a Console application that runs independently. Therefore, when debugging Asp. Net Core deployed in IIS, It is not attached to the w3wp process as before, but to run the projectDotnetProcess (run by dotnet watch run ).

...... There are four dotnet processes at once. Which one is it? I don't know, but I haven't found out why after a long time. It can be determined that it is affected by arguments = "watch run:

Please kindly advise if you have any questions. Thank you!

Reference

Source code: https://github.com/linianhui/aspnetcore/tree/master/dotnet-watch-run

AspNetCoreModule: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/aspnet-core-module? Tabs = aspnetcore2x

AspNetCoreModule Config: https://docs.microsoft.com/en-us/aspnet/core/hosting/aspnet-core-module

Kertrel: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel? Tabs = aspnetcore2x

Dotnet watch: https://docs.microsoft.com/en-us/aspnet/core/tutorials/dotnet-watch

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.