Create windows service using Topshelf

Creating a windows service using Topshelf is as easy as creating console application. Just create a console application, configure TopShelf and you are done. Sounds pretty yeah? So let us understand this pretty thing in details.

What is Topshelf?

Topshelf is a third party windows service framework in .net. Using Topshelf, it is easy to run and debug windows service. Once you create a windows service using Topshelf, you can host it as service, as a task scheduler or you can directly run console exe. That is what I like about Topshelf. So let us put Topshelf into code step by step.

 

We will create simple windows service using Topshelf that will write date and time in text file.

Step-1: Create console application

Step-2: Install Topshelf

Step-3: Service functionality implementation

Step-4: Configure Topshelf

Step-5: Service implementation to write date and time in text file.

Now we will start creating windows service step by step:

Step-1: Create a console application first.

  1. Open visual studio
  2. Create new project : File -> New -> Project

windows service using TopShelf - New Project

  1. Under new project window, Choose console application and give a name ‘SampleWindowsService’

windows service using TopShelf - Windows Service

  1. Now if you see in solution explorer it will look something like this

windows service using TopShelf - Solution Explorer

Step-2:  Install Topshelf from NuGet package:

  1. Right click on your project and click on ‘Manage NuGet Packages’ as shown below:

windows service using TopShelf - Manage nuget packages

  1. In ‘Manage NuGet Package’ window search for the Topshelf and click on install button

windows service using TopShelf - Select Nuget Package

  1. Once Topshelf is installed, go to next step

Step-3: Implement Service Logic:

  1. Now create a new class file ServiceManager.cs : right click on project -> add -> class. This ServiceManager class will contain actual implementation of our service.
  1. First of all, In ServiceManager.cs class, we will define two void methods StartService and StopService:

 This two methods will contain implementation of the code of what to do when service will get started and stopped.

Note: We will write actual code in these methods in Step5.

Step-4: Configure Topshelf

Now open Program.cs file and configure Topshelf in main method as shown below:

Okay okay don’t scratch your head. It’s a very simple configuration. Let us understand it step by step.

  1. HostFactory.New will configure new service host for your windows service
  2. Configurator is an instance of the lambda expression using which you can expose host level configurations
  3. Service<ServiceManager> will inform Topshelf that we are using service of type ‘ServiceManager’
  4. ConstructUsing will create a new instance of class ServiceManager
  5. WhenStarted will call the method ‘StartService’ of class ServiceManager. Basically an action which needs to be performed when service is started. Here in our example. Logic to write date and time in text file at every 1 minute.
  6. WhenStopped will call method ‘StopService’ of class ServiceManager to perform an action when service is stopped.
  7. RunAsPrompt() will prompt for a username/password to launch the service. There are many other options like RunAs(), RunAsNetworkService(), RunAsLocalSystem(), RunAsLocalService() which are documented here
  8. SetDescription() will be used to mention small description of the service which will be displayed in Service window after installation. See: Image-Service Window
  9. SetDisplayName() is used to display the name of the service in service window
  10. SetServiceName() is used to set the service name.
  11. EnableServiceRecovery: When service is crashed due to some reason than it will restart service after one minute.
  12. Run() will run the host which we configured using hostfactory.new(). And it will return a service exit code when it is exited/stopped. There could be many reasons of service exit as shown below image and you can perform action based on individual reason:

windows service using TopShelf - service exit codes

  1. Here I send an email to the administrator if it is not exited with exit code OK (Exit code will be OK when service is stopped gently).

Step-5: Service implementation to write date and time in text file:

Now go to the class ServiceManager.cs.  As we want to write current date and time at every minute, we will define timer  and initialize it as mentioned below:

 As written in above code, at every second (1000 milliseconds) timer_Elapsed event will be fired and write the date and time in text file.

And Now in final step, we’ll write a code to start the timer when service gets started  and the same to dispose the timer when service is stopped as shown below:

You are done. After completion of above step, you have created a windows service using Topshelf to write a date and time in log file at every second. You can directly run a console application to debug or run windows service. You can download ServiceManager.cs and Program.cs from here,

If you need to install this service on server, than please have a look at Topshelf document to install/uninstall service:

Please feel free to ask if you have any query or concern.