Creating a Windows service in .NET with ease

Introduction

This time I chose to talk about a not very trending theme. Topshelf doesn’t seem to be a very fancy theme, but you may even find a course in Pluralsight about it.

Topshelf has already made it easier for many devs to create services. I’ve been looking at some FOSS frameworks on GitHub and this is one of the ones I like the most.

With the concepts in this tutorial and some minor changes, you’ll create many cool applications such as:

  • A folder synchronized like OneDrive
  • Your own CD pipeline locally
  • Handle incoming files from a FTP
  • And more…

What is Topshelf?

Topshelf is an excellent framework for hosting services in which you can write console applications in .NET that can be installed as a service. The development workflow becomes very smooth because you can debug and test your services like any other console application before you install it in a production environment.

By using Topshelf, all the complexity of developing a standard service is abstracted from you even in the installation process. Topshelf also supports Mono on Linux but we will going to cover the Windows only here.

Let’s get started

  1. Open Visual Studio 2017 (btw I’m using the preview 2) and create a new Console Application project.

    Figure 1. Creating the project.
  2. Open the Package Manager Console window from the menu View / Other Windows / Package Manager Console.
  3. Install these two packages below by typing the following commands:

Listing 1. Installing Topshelf with NLog.

Listing 2. Installing NLog.Config.

In order to get your service up and running, you only need to install the Topshelf package alone. But as we’ll talk about logging later, we’ll install these alternate packages.

  1. Now we will create our ConfigureService class like shown:

    Figure 2. Creating the ConfigureService class.
  2. Before proceeding with our configuration class, we’ll create another class called FileWatcherService and add the following code to begin:

    Listing 3. FileWatcherService class.

  3. Now, we can continue to implement our ConfigurationService class in a fluent way. This first configuration will look like this:

    Listing 4. The ConfigurationService class implementation.

With all this set, we can run the project for the first time and see the result. So hit F5 and Voilà!

If you want to install your service now, simply enter the bin/debug folder, create and run this script there (name it to something like install.bat):

Listing 5. Installing the service.

In order to confirm that your service has been installed successfully press Windows+R, type services.msc and hit Enter. You should find you service somewhere.

Logging

Unit testing, design documentation, logging – all of these are secondary development activities that we know we really should be doing. Devs frequently write applications that require logging functionality. Topshelf uses a TraceSource for logging by default, with no additional dependencies. But, as you can see at the beginning, we have already installed the Topshelf.NLog and NLog.Config NuGet packages that allows us to use NLog integration in our project.

So this is how our NLog.Config configuration file will look like:

Listing 6. The NLog.Config file.

Now let’s make small changes to our code to implement the logging system.

Listing 7. The TopshelfFileWatcher class updated.

Run the application with F5 and check out your info message in the log.txt file.

After that, change the class ConfigureService as shown below and run the application again. When you check the log file again, you will see that Topshelf is now using NLog as well.

Listing 8. Using NLog.

Well, this is just a small demonstration of the workflow with Topshelf. There are some other interesting settings that can be explored that were not mentioned in this tutorial. For more information visit Topshelf Configuration.

You can download the complete source code of this tutorial on my GitHub repo.

Thank you for your time!

Last edited on Oct, 07, 2017 at 11:03 pm