Creating Windows Service In .NET with Topshelf

Microsoft Windows Services/NT services, allow us to create long-running applications that run in the background repetitively without the need of any user interface or user interaction like Windows Forms Application, WPF Application, Console Application.

There are different ways to create Windows Services applications using the .NET Framework, but the one way is to use open source project Topshelf.

Topshelf is an open source Windows service framework for the .NET platform that makes it easy to create a Windows service.

Why would I use Topshelf to create Windows service?

It makes it easy to create a Windows service as well as test the service, debug the service, and install it into the Windows Service Control Manager (SCM) with simple commands.

By using Topshelf, we don’t need to understand the complex details of service classes, perform installation via InstallUtil, or learn how to attach the debugger to services for troubleshooting issues.

With Topshelf, creating a Windows service is as easy as creating a console application. Once the console application is created, we need to create a single service class with public Start and Stop methods and configure asa  service using Topshelf’s configuration API.

Now, we have a complete Windows service that can be debugged using the debugger (just by pressing F5) and installed using the Topshelf command (install/uninstall).

Here is link to Topshelf open source project documentation.

Following are the steps to create Windows service in .Net with Topshelf:
  1. Create a New Project with Console Application Template using Visual Studio.

    Console application

  2. Install Topshelf using NuGet Package Manager.

    NuGet Package Manager

  3. Create a class that contain our service logic: MyService.cs.
    1. namespace WindowsServiceWithTopshelf  
    2. {  
    3.     public class MyService  
    4.     {  
    5.         public void Start()  
    6.         {  
    7.             // write code here that runs when the Windows Service starts up.  
    8.         }  
    9.         public void Stop()  
    10.         {  
    11.             // write code here that runs when the Windows Service stops.  
    12.         }  
    13.     }  
    14. }  
  4. Create a class to configure our service logic using the Topshelf project: ConfigureService.cs.
    1. using Topshelf;  
    2. namespace WindowsServiceWithTopshelf  
    3. {  
    4.     internal static class ConfigureService  
    5.     {  
    6.         internal static void Configure()  
    7.         {  
    8.             HostFactory.Run(configure =>  
    9.             {  
    10.                 configure.Service < MyService > (service =>  
    11.                 {  
    12.                     service.ConstructUsing(s => new MyService());  
    13.                     service.WhenStarted(s => s.Start());  
    14.                     service.WhenStopped(s => s.Stop());  
    15.                 });  
    16.                 //Setup Account that window service use to run.  
    17.                 configure.RunAsLocalSystem();  
    18.                 configure.SetServiceName("MyWindowServiceWithTopshelf");  
    19.                 configure.SetDisplayName("MyWindowServiceWithTopshelf");  
    20.                 configure.SetDescription("My .Net windows service with Topshelf");  
    21.             });  
    22.         }  
    23.     }  
    24. }  
  5. Call configure service in Main():
    1. namespace WindowsServiceWithTopshelf  
    2. {  
    3.     class Program  
    4.     {  
    5.         static void Main(string[] args)  
    6.         {  
    7.             ConfigureService.Configure();  
    8.         }  
    9.     }  
    10. }  
  6. Build Console application.

  7. Install/uninstall Console Application using Topshelf command.

    1. Run Command Prompt as an Administrator and navigate to the bin/debug folder of Console Application.

      > WindowsServiceWithTopshelf.exe install


    2. To uninstall windows service run this command.

      > WindowsServiceWithTopshelf.exe uninstall

  8. How to Debug Window service created using Topshelf?

    Just by pressing F5 like console application.

Read more articles on Windows Services: