AspNetCore.Docs/aspnetcore/host-and-deploy/windows-service.md

6.0 KiB

title author description manager ms.author ms.custom ms.date ms.prod ms.technology ms.topic uid
Host in a Windows Service tdykstra Learn how to host an ASP.NET Core application in a Windows Service. wpickett tdykstra mvc 03/30/2017 aspnet-core aspnet article host-and-deploy/windows-service

Host an ASP.NET Core app in a Windows Service

By Tom Dykstra

The recommended way to host an ASP.NET Core app on Windows without using IIS is to run it in a Windows Service. That way it can automatically start after reboots and crashes, without waiting for someone to log in.

View or download sample code (how to download). See the Next Steps section for instructions on how to run it.

Prerequisites

  • The app must run on the .NET Framework runtime. In the .csproj file, specify appropriate values for TargetFramework and RuntimeIdentifier. Here's an example:

    [!code-xml]

    When creating a project in Visual Studio, use the ASP.NET Core Application (.NET Framework) template.

  • If the app receives requests from the Internet (not just from an internal network), it must use the WebListener web server rather than Kestrel. Kestrel must be used with IIS for edge deployments. For more information, see When to use Kestrel with a reverse proxy.

Getting started

This section explains the minimum changes required to set up an existing ASP.NET Core project to run in a service.

  • Install the NuGet package Microsoft.AspNetCore.Hosting.WindowsServices.

  • Make the following changes in Program.Main:

    • Call host.RunAsService instead of host.Run.

    • If the code calls UseContentRoot, use a path to the publish location instead of Directory.GetCurrentDirectory()

    [!code-csharp]

  • Publish the application to a folder.

    Use dotnet publish or a Visual Studio publish profile that publishes to a folder.

  • Test by creating and starting the service.

    Open an administrator command prompt window to use the sc.exe command-line tool to create and start a service.

    If the service is named MyService, publish the app to c:\svc, and the app itself is named AspNetCoreService, the commands would look like this:

    sc create MyService binPath="C:\Svc\AspNetCoreService.exe"
    sc start MyService
    

    The binPath value is the path to the app's executable, including the executable filename itself.

    Console window create and start example

    When these commands finish, browse to the same path as when running as a console app (by default, http://localhost:5000)

    Running in a service

Provide a way to run outside of a service

It's easier to test and debug when running outside of a service, so it's customary to add code that calls host.RunAsService only under certain conditions. For example, the app can run as a console app with a --console command-line argument or if the debugger is attached.

[!code-csharp]

Handle stopping and starting events

To handle OnStarting, OnStarted, and OnStopping events, make the following additional changes:

  • Create a class that derives from WebHostService.

    [!code-csharp]

  • Create an extension method for IWebHost that passes the custom WebHostService to ServiceBase.Run.

    [!code-csharp]

  • In Program.Main change call the new extension method instead of host.RunAsService.

    [!code-csharp]

If the custom WebHostService code needs to get a service from dependency injection (such as a logger), get it from the Services property of IWebHost.

[!code-csharp]

Next steps

The sample application that accompanies this article is a simple MVC web app that has been modified as shown in preceding code examples. To run it in a service, do the following steps:

  • Publish to c:\svc.

  • Open an administrator window.

  • Enter the following commands:

    sc create MyService binPath="c:\svc\aspnetcoreservice.exe"
    sc start MyService
    

If the app doesn't start up as expected when running in a service, a quick way to make error messages accessible is to add a logging provider such as the Windows EventLog provider.

Acknowledgments

This article was written with the help of sources that were already published. The earliest and most useful of them were these: