Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Running an ASP.NET Core Application as a Windows Service

DZone's Guide to

Running an ASP.NET Core Application as a Windows Service

We can now run ASP.NET Core applications as Windows services with minimal effort. This blog post introduces how this process is done.

· Web Dev Zone ·
Free Resource

Deploy code to production now. Release to users when ready. Learn how to separate code deployment from user-facing feature releases with LaunchDarkly.

ASP.NET Core 2.1 introduces a new application host for Windows services. We can now run ASP.NET Core applications as Windows services with minimal effort. This blog post introduces how it is done and how to build and run Windows services on ASP.NET Core without any need for dirty hacks.

Creating a Default Web Application

We start with new default ASP.NET Core 2.1 web application.

I didn't configure HTTPS as this is a sample application and it does nothing important.

By default, the Program class looks like this.

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

Now we have working default application and it's time to focus on the Windows service.

Running an ASP.NET Core Application as a Windows Service

Running the application as a Windows service takes some effort before we can open the browser and see it running under the service. First, we have to specify a runtime for our application, as ASP.NET Core also supports operating systems and architectures where Windows services don't run. For this, we have to modify the project file.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RuntimeIdentifier>win7-x64</RuntimeIdentifier>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

</Project>

Next, add a reference to the NuGet package Microsoft.AspNetCore.Hosting.WindowsServices. This package has everything needed to run an ASP.NET Core application as a Windows service.

NB! For me, the newest version 2.1.1 of Microsoft.AspNetCore.Hosting.WindowsServices conflicted with ASP.NET Core 2.1.0 and I went with version 2.1.0 instead.

We also have to modify the Main() method of the Program class. In its simplest form, the Main() method looks like this.

public static void Main(string[] args)
{
    var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
    var pathToContentRoot = Path.GetDirectoryName(pathToExe);

    var host = WebHost.CreateDefaultBuilder(args)
        .UseContentRoot(pathToContentRoot)
        .UseStartup<Startup>()
        .Build();

    host.RunAsService();
}

All we have to do now is to publish our application, register it as a Windows service, and start the service.

Running the Application as a Service or on a Console

Those who have built Windows services before, know very well that the debugging of services can be a pain in one specific anatomical area as after building the service one has to deploy new version of it, attach a debugger, etc. There is simple way around - we make our Windows service run also as a console application that is easy to run on debugger from Visual Studio.

We can apply the same trick also with ASP.NET Core application that is running as Windows service.

public static void Main(string[] args)
{
    var isService = !(Debugger.IsAttached || args.Contains("--console"));
    var pathToContentRoot = Directory.GetCurrentDirectory();
    var webHostArgs = args.Where(arg => arg != "--console").ToArray();

    if (isService)
    {
        var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
        pathToContentRoot = Path.GetDirectoryName(pathToExe);
    }

    var host = WebHost.CreateDefaultBuilder(webHostArgs)
        .UseContentRoot(pathToContentRoot)
        .UseStartup<Startup>()
        .Build();

    if (isService)
    {
        host.RunAsService();
    }
    else
    {
        host.Run();
    }
}

The code shown may seem a little tricky. Here are my explanations:

  • To run the application on the console from Visual Studio we control if the debugger is attached. If we want to run the application as a console application outside of Visual Studio we can use the -console argument.
  • When the application runs as a web application under the web server, we must use the current directory as the content root. But when the application runs as a service, we need an executable path as the content root.
  • We remove the -console argument, as ASP.NET Core expects all arguments to be name-value pairs.

Now try to run the application from Visual Studio. It starts as a usual web application.

Making the Application Run as a Windows Service

To make the application run as Windows service we need some additional steps.

  1. Publish the application to some folder.
  2. Open the command line in administrative permissions.
  3. Register the application as a Windows service using t command (space after "binPath=" is mandatory)  sc create AspNetWindowsService binPath= "path to my application exe"
  4. Start service: sc start AspNetWindowsService.
  5. When the service starts, open the browser and navigate to http://localhost:5000 to see the web application running.

Before releasing a new version of the service, the currently running instance must be stopped. For this, we can use the command sc stop AspNetWindowsService. To remove the service, run the following command: sc delete AspNetWindowsService.

Wrapping Up

With the new Microsoft.AspNetCore.Hosting.WindowsServices NuGet package, it is easy to run ASP.NET Core applications as Windows services. We had to modify project file a little bit and make the application's Main() method in order to understand if an application runs as a service or a console application. Using this trick we are able to build a web application on Visual Studio and run it as a typical web application. Our burden to get a web application running as Windows service was minimal.

Deploy code to production now. Release to users when ready. Learn how to separate code deployment from user-facing feature releases with LaunchDarkly.

Topics:
web dev ,asp.net core ,web application development ,windows services

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}