Blog #4

Blog #1

 8 May 2017

Sometimes you need to have a process that runs continually in the background on your server or infrastructure 24×7.

Windows Services can help you do this and are easy to build in .NET.  You write your custom code, implement the OnStart/OnStop events and setup your Timer to fire said custom code at specific intervals.

This is fine and works well.

The bit that I find to be a pain as I’m going through the development phase is the install / uninstall process as you build functionality.

It’s often done through the developer command prompt using the following syntax:

Install

InstallUtil.exe "c:\myservice.exe"

UnInstall

InstallUtil.exe /u "c:\myservice.exe"

It’s not a massive amount of effort but continually doing this as you develop/debug your Windows service it soon starts to frustrate.

Enter Topshelf

Topshelf is an open source Windows Service framework that saves the developer from having to go through the install/uninstall process repeatedly.  I’ve found Topshelf speeds up the development/debugging cycle as I’m not messing around with batch files or the command prompt just to test a few amendments to my Windows service.

Nor do I have to attach my Visual Studio debugger to a Windows Service process!

You can get Topshelf from the NuGet Gallery here.

How does it Work?

Topshelf is easy to use, it allows you  develop your Windows service as a Console Application.  The main steps are:

  1. Create a Console application and add the Topshelf NuGet package to it.

  2. Create a Class to Contain the Service logic class (this needs to implement OnStart and OnStop events).

  3. Setup Topshelf configuration.

How do I implement it?

You can find more detailed information to help you configure Topshelf on the project site but the following code snippets will give you the basics in terms of implementing it very quickly.

These code snippets show you the basic Topshelf Configuration within a Console Application that invokes a custom Service Class.  I used something like this when building/testing a Windows Service that would extract content periodically from social media channels.

In this code, you can see I’ve setup the OnStart OnStop events as well as a Timer to execute my code every few minutes.  This is the service (your Windows service!) that will be started when the Topshelf configuration is loaded.

Testing your new service is simple now.  Simply place your breakpoints and hit F5 to run the Console Application and debug the code that forms your Windows Service.

No more command line/batch files or installers whilst you refine your Windows Service.

When you complete development of your Windows Service you can migrate this code into your Windows Service project.

Have you used Topshelf and found it useful?

 

If you'd like to find out more, click the image below to Download 'Make Your Code More Readable, Secure & Maintainable'.

Or View Our Diary And Book In A Call

Also included:

  • ASP.NET Patterns & Practices (MVC)
  • How To Implement Dynamic Business Rules In Your Application
  • Should You Use Workflow Foundation In Your Project

Comments

The "call to action" button above says... "Make Your Code More Readable, Secure & MAINTANABLE" - oops!
Posted on Wednesday, February 21, 2018 10:17 by Barry

Post Comment

*
*
*