How to host ASP. NET Core applications to Windows Service, asp. netcore

Source: Internet
Author: User

How to host ASP. NET Core applications to Windows Service, asp. netcore

(This article is also published on my public account "dotNET development experience". You are welcome to follow the QR code on the right .)

Question: a middleware design is being conceived to determine whether the latest technology can be used or the traditional deployment mode can be used. Therefore, we have proposed and solved this problem (including derivative problems.

Hosted in Windows Service

As we all know, ASP. NET Core adopts different hosting and HTTP processing methods than traditional ASP. NET, that is, completely decoupling servers from hosting environments.

ASP. NET Core has two built-in HTTP Server implementations, one is Kestrel Based on libuv (supports cross-platform), and the other is WebListener Based on Windows HTTP Server API (only supports Windows ).

The hosting environment can be unrelated to the server. Generally, it is self-managed, or host it in IIS/IISExpress (here IIS only forwards requests to the server used as a reverse proxy ).

Therefore, we plan to deploy ASP in a more traditional way like Windows Service. NET Core Web applications are also feasible (in essence, it is self-managed, but the startup process is not a console program, but a Windows Service ). This is not the case. Microsoft provides a Nuget to support: Microsoft. AspNetCore. Hosting. WindowsServices. Its source code is.

It is also easy to use:

public static class CustomWebHostWindowsServiceExtensions{    public static void RunAsCustomService(this IWebHost host)    {        var webHostService = new CustomWebHostService(host);        ServiceBase.Run(webHostService);    }}host.RunAsCustomService();

Hosting ASP. NET Core applications in Windows Service is that simple!

More questions?

However, I want to talk about my scenario and why I NEED TO HOST Windows Service. In the past few days, a middleware (including multiple components) architecture has been conceived. Considering that the architecture will be deployed in a traditional way in the early stage, it is possible to implement a cross-platform architecture in the later stage, we also hope that components can be relatively independent and decoupled. Therefore, the most natural idea is that the architecture is designed for microservices and implemented based on ASP. NET Core (other technology stacks will not be excluded in the future ). If deployed, multiple Windows Services are deployed separately in the initial stage, and the container or microservice running platform similar to Service Fabric can be smoothly used in the later stage.

Based on such design considerations, the first problem to be solved is whether ASP can be applied. the NET Core application is hosted in Windows Service (verified above). The second problem is whether the application can run in different startup processes according to the environment conditions, the third question is whether multiple Runtime is supported at the same time. Two or three problems are actually very simple to solve.

Different startup modes are supported.

The solution to the second problem is as follows:

It's so simple and rude.

Support different Runtime

. NET Core originally supports multiple runtime tasks of a project, even if net46 and netcoreapp1.0 are mixed. The specific method is as follows:

Sample source code

In order to avoid posting a large segment of source code in the article, you go to GitHub to see the sample code: https://github.com/heavenwing/HostingAspCoreAsWindowsService

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.