Introduction
In this article, we will be discussing how to deploy & host ASP.NET Core 3.1 Web API as a Windows Service. You may have one question in mind: why host applications as a Windows service, and why not on IIS? So in this article, we will see reasons behind hosting applications as a Windows service. We will also be creating a Web API & hosting it as a Windows service. So let's grab a cup of coffee and start coding.
What is a Windows Service?
According to the Microsoft documentation:
Microsoft Windows services, formerly known as NT services, enable you to create long-running executable applications that run in their own Windows sessions. These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface. These features make services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer. You can also run services in the security context of a specific user account that is different from the logged-on user or the default computer account.
In most scenarios where we have to make a long-running application, then Windows service is the best option. Windows services require an exe i.e executable of our application.
Why deploy Applications as a Windows Service?
When we create an application, we have to host it somewhere so that users can access it. We can either host it on IIS or as windows service. So below are a few reasons for hosting applications as a Windows service:
- Sometimes we host our applications on IIS, but we don't utilize the full features of IIS.
- If the machine where we are hosting web application does not have IIS enabled, or if it IIS enabled but not configured to host a .NET Core application.
We have already discussed we require executable for hosting application as Windows service. So to do this, .NET Core provides one deployment mode called Self-contained deployment (SCD). When we published our app as SCD, then it will provide the executable of our app along with .NET Core runtime DLLs. If you don't know about the different hosting and deployment models in .NET Core, then you can check out my below articles:
Hosting ASP.NET Core 3.1 Web API as a Windows service
So now its time to actually host an application as a Windows service. First, we have to create basic ASP.NET Core 3.1 Web API. Those who don't know how to create it, then follow below steps. Open Visual Studio 19 and also make sure .NET Core 3.1 is installed on your machine. Create a new project and select ASP.NET Core Web Application template and click on next:

Give a proper name to your application and click on the Create button,

Select ASP.NET Core 3.1 in the dropdown, select API and click on the Create button,

That's it! We have created our Web API.






Hamid KhanPosted Dec 17, 2020, 2:45 AM
Nice explanation and useful information............
Khushbu SainiPosted Sep 21, 2020, 12:41 AM
Can you please tell me how to host this website to live domain server because it throughs an error ( Error 505 ) while we published site by self contained mode.
R JPosted Apr 30, 2020, 10:15 AM
Very terse article.Beautiful.
Simon ShPosted Mar 2, 2020, 1:05 AM
Nice but how do you 1) get parameters in from the underlying OnStart (per next Q)? 2) how do you set up the service Start Parameters in registration? I wish we all had simplistic windows services to write!!!
Abhishek MishraPosted Feb 18, 2020, 11:33 PM
Nice Article and well explained!!