Did you know that we can host ASP.NET service on Windows with Windows Service without using IIS? The benefit of hosting on Windows Service is that the application will automatically restart after the server reboot.

This functionality is implemented using a Worker Service Template that becomes the initial point for writing and building long-running service applications. We are focusing on a solution without the IIS because the same may not be available at all times.

Even in the situations where you have IIS already set up, it may not host .NET Core applications, let alone ASP.NET Core. Hence, to host them, you need to understand the implementation of the .NET Core web application in Visual Studio.

Why Use a Web App as a Windows Service?

In situations where you want to host .NET Core applications without IIS, we can work with self-contained deployment. Here, the application basically runs as a .exe file.

Follow these steps to set up a hosting environment for ASP.NET 3.1 in Windows Service.

1. Create new ASP.NET Core 3.1 Application in Visual Studio

Create a new project in VS2019 .NetCore web application. Specify version 3.1 as you create a new project in the folder (follow the screenshot).

2. Running an ASP.Net Core application as a Windows Service

Following the first step, specify a runtime for the application. ASP.NET Core also supports operating systems where Windows Services don’t run.

For this, we must modify the project file. To modify the project file, double click on the Project name and add the following two lines as per the screenshot.

Add a reference using NuGet package Microsoft.ASPNetCore.Hosting.WindowsServices and Newtonsoft.json.

This Package has everything needed to run an ASP.Net Core application as a Window service.

Now, run the application as Windows Service and configure it in the Program.cs file. In case you want to run an application as IIS Express, then there is no need to change any configuration setting, and we can directly run an application as IIS Express.

We can specify a different port in the appsettings.json and appsettings.Devlopment.json file to run an application on this port directly using the Windows service.

Specify Port no 5009.

appsettings.json File:

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
	}
  },
"AllowedHosts": "*",
"ServicePort" :  5009
}
 appsettings.Devlopment.json file:
{
"DetailedErrors": true,
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
	}
  },
"ServicePort": 5009

Moving on, to specify our appsettings.json file as Configuration path in Program.cs file to let the application read that path and run on that port. See the screenshots below.

Program.csFile:

3. Build the application and Publish the code

With the above-given configuration completed in the Program.cs file start building the application, followed by publishing the code in the folder.

4. Making the application Run as WindowsService

To make the application run as a Windows service follow these steps;

  • Open the Command line with administrative Permission.
  • Register the application as a Windows service using this command (space after “binPath=” is mandatory)

Create windows service using the following command:

sc create AspNetCoreWindowsServicebinPath = “path to my publish folder application exe –service”

In the example above, we have taken AspNetCoreWindowsService as the name; you can use any name you want to keep. Also, –service is an argument name that we pass in Program.cs file.

Nest, open the services.msc and see your hosted Windows service with the name AspNetCoreWindowsService to start the service.

When the service starts, open the browser and navigate to http://localhost:50009 to see the web application is running. If you would remember, we had set port 5009 in the earlier steps of ASP.NET core web application development. Specifically, look for the part where we had specified it in the appsetting.json file as mentioned above.

Delete windows service using the following command:

Stop the existing running instance before releasing the new version of the service. Once done, write the following command.

scdeleteAspNetCoreWindowsService

Again, AspNetCoreWindowsService is the windows service name that we have set to give you an example.

Conclusion

This article takes you through the steps to set up a hosting environment for ASP.NET Core 3.1 web application development in Windows Service. Although the process is simple enough to understand, we at DEV IT have understood the nuances of the same along with knowing everything about agile development practices.

Leave a comment

Your email address will not be published.