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?
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.
The next step is we have to install a NuGet package.
or...
run the below command in the Nuget package manager console:
- Install-Package Microsoft.Extensions.Hosting.WindowsServices
Now there is only one line of code for converting a Web API as a Windows service. Open your Program.cs, and you will see the CreateHostBuilder method so add UseWindowsService() at the end of the method.
And that’s all the code changes required.
Now the next step is to deploy the application in SCD mode. So right-click on the application and select Publish option.
Select a publish target as Folder and click on the Advanced button.
Select Deployment mode as Self-contained and Target Runtime as win-x64. Click on Save and then click on the Create profile button.
Finally, click on the Publish button to publish the app. You can also publish your app using dotnet CLI by running the below command:
- dotnet publish -c Release -r win-x64 --self-contained
Go to bin\Release\netcoreapp3.1 and you will find the win-x64 folder which contains our published dlls.
To create a Windows service, open a command prompt in administrator mode and use the below command:
- sc create <name of service you want to create> binPath= <path of executable of your app>
So we will run the command as:
- sc create WindowsServiceDemo binPath= "C:\Projects\WindowsServiceDemo\bin\Release\netcoreapp3.1\win-x64\WindowsServiceDemo.exe"
So our service WindowsServiceDemo is created.
Right-click on service and click on start. So our Web API is running on URL http://localhost:5000. Our API has only one controller at present so to check whether we will get output hit the URL http://localhost:5000/weatherforecast in a browser and you will see the response:
We have successfully hosted our ASP.NET Core 3.1 Web API as Windows service.
Conclusion
In this article, I have explained what a windows service is, and the reasons for hosting applications as a windows service. Also, I demonstrated how to host the ASP.NET Core 3.1 Web API as Windows Service. I really hope that you enjoyed this article, share it with friends and please do not hesitate to send me your thoughts or comments.