Demystifying State Machines
“Delorean." That single word makes you think of a time machine and an unimaginable number of possible time travel destinations.…Read More
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.
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:
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.
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:
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.
Topshelf is easy to use, it allows you to develop your Windows service as a Console Application. The main steps are:
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.
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.
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.
Where you see “custom code”, you’d simply execute your API method there.
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:
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:
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:
Machine learning (ML) algorithms have had a considerable impact on the technology industry over the past several years. Their applications…Read More