How to Speed Up Windows Service Software Development in C#

How to Speed Up Windows Service Software Development in C#
Reading Time: 5 minutes

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 set up your Timer to fire said custom code at specific intervals.

You might have a process that fetches data at regular intervals from an external system and imports it to your local database or a system check that executes every few seconds.  Windows Services are great for these sorts of tasks.

Download our guide to learn how you can speed up the Windows Service Development in C# using Topshelf.

The Pain

Installation and uninstallation process can soon start to frustrate you.

The bit that can be a pain as you go through the development phase, is the installation and uninstallation process as you build the functionality that’s required by your service.

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

Install
 

 
UnInstall
 

It’s not a massive amount of effort but continually doing this as you develop/debug your Windows service, can soon start to frustrate you as the developer.

Enter Topshelf

Topshelf is an open-source Windows Service framework that saves you, as the developer, from having to go through the install/uninstall process repeatedly.  It’s written using the .NET Framework and as a developer, you’ll find that Topshelf:

  • speeds up the development/debugging cycle
  • no more messing around with batch files
  • no more messing around with the command prompt just to test a few amendments
  • no more attaching your Visual Studio debugger to a Windows Service process!
  • small footprint – around 200KB with NO dependencies

You can get Topshelf from the NuGet Gallery here.  The simplest way to install Topshelf is by going into your Visual Studio Project or solution and running the following command:

At the time of writing, Topshelf works with Mono which makes it possible to build services for Linux but the service installation features only target Windows at the minute.

How does it Work?

Topshelf is easy to use, it allows you to 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.
  4. Click Run in Visual Studio!

Download our guide to learn how you can speed up the Windows Service Development in C# using Topshelf.

How do I implement it?

Basic Topshelf Configuration within a Console Application.

Basic Topshelf Configuration within a Console Application that invokes a custom Service Class

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 the OnStart OnStop events as well as a Timer to execute 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 have completed the development of your Windows Service you can migrate this code into your Windows Service project.

Final Tips

API that calls into the OnStart method of your Windows Service

So now you have an overview of how you can speed up .NET Windows Service development.  There are some other techniques you can employ to speed up the development of these components.
If for example, you are working on software product with many business rules, encapsulate these rules and processes in an API or class library.

Why?

By doing this, you’ll be able to reuse this class library across multiple solutions, this could be anything from a web application, WPF application or, you guessed it – a Windows Service application.
If you already have a pre-built (and system tested) API, you can simply slot this API and its respective method calls into the OnStart method of your Windows Service.  The end result is a Windows Service that has very little code in it and is therefore very easy to debug.

Remember this?

 
Where you see “custom code”, you’d simply execute your API method there.

Logging

If you wish to add error logging to help you debug your Topshelf service, you can do this without installing any additional components.  Topshelf uses TraceSource by default which ships as part of the .NET framework.
Other logging providers are supported such as log4net.  To enable logging via log4net, you need to download the Topshelf.Log4Net NuGet package.  When you have this added to your project, you simply configure Topshelf to use it by adding the following code:

Service Identity

Passing credentials to the configuration option to let service run as a different user.

You might want your Service to RunAs a different user.  Topshelf supports that too!  You simply pass the credentials into the configuration option:

You can find more about other configuration options on the Topshelf documentation site here where you’ll see advanced options that allow you to:

  • introduce service dependencies
  • enable pause and continue
  • enable service shutdown
  • determine what to do when an exception has occurred
  • and much more!

Download our guide to learn how you can speed up the Windows Service Development in C# using Topshelf

Summary

In this article, we’ve discussed how you can speed up Windows Service development using .NET, and some other ideas to consider when building your software solutions that can also help.

At Growth Acceleration Partners (GAP), we can help you speed up the process by building .NET solutions for you!

Our software development teams have been writing web applications in C# and ASP.NET for years. Based in Austin, TX, our .NET developers are located nearby in Latin America with primary development centers in Costa Rica and Colombia.

With this proximity, GAP has become the go-to outsource partner for .NET development. With similar timezones and a culture that mirrors Western values, GAP has satisfied hundreds of US-based companies.

GAP’s .NET developers deliver on time in a cost-effective manner solving business problems for our partners. We can provide your organization with a team of highly qualified data scientists and engineers who have expertise in:

  • .NET framework including C#, ASP.NET
  • Windows Communication Foundation (WCF)
  • Windows Presentation Foundation (WPF)

You can find out more about our .NET services on our website here.
To speak to someone directly, feel free to arrange a call with us by sending us an email.