Course info
Feb 12, 2016
1h 40m

Windows Services are a method by which we can deploy code and have it running in the background without needing to manually log-in and start console applications, for example. We can write Windows Services in .NET but developing, testing, debugging, and installing Windows Services can be tricky. Topshelf is an open source library that greatly reduces the friction of developing, testing, and installing Windows Services.

About the author
About the author

With over 15 years experience, Jason Roberts is a Microsoft .NET MVP, freelance developer, and author.

More from the author
Mocking with Moq and NUnit
1h 53m
Feb 22, 2019
Introduction to .NET Testing with NUnit 3
2h 6m
Dec 17, 2018
More courses by Jason Roberts
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hi everyone. My name's Jason Roberts. Welcome to my course, Getting Started Building Windows Services with Topshelf. I'm a Microsoft. NET MVP, freelance developer, and author. Windows services are a great way to run code in the background without needing a user to be logged in or having to manually run a console application. They can be automatically started when the system boots up, be configured to run as specific user logons, and automatically restart if errors occur. Some of the major topics that we'll cover include installing Topshelf, writing service classes, debugging and testing service code, installing services into Windows, and configuring features such as service, pause, and continue, automatic service recovery, and automatic service startup. By the end of this course you'll know how to easily write and install Windows Services that can take advantage of the many features of the Windows services infrastructure. Before beginning the course you should be familiar with using Visual Studio to write C# code. Well I hope you'll join me on this journey to learn how to get started building Windows services with the Getting Started Building Windows Services with Topshelf course at Pluralsight.

Developing a Windows Service from Scratch
Hi, welcome back. In the previous module we got an overview of Windows services and we learned about some of the features that are available to us if we're making use of the Windows services infrastructure. In this module we're actually going to get started and develop a Windows service from scratch. So in this module we're going to start off by looking at a typical Topshelf development lifecycle process, so we'll see some of the different stages involved when we're developing a Windows service using Topshelf, and some of the benefits we get, such as not having to install the service into Windows every time just so we can test it. So we'll start off by introducing Topshelf's fluent API, and this fluent API helps us configure what our service is going to do when it starts and stops for example, and we can also configure some of the service properties such as the service name. Next we'll jump into Visual Studio and we'll start developing our first Topshelf Windows service. To do this we'll start off by creating a new class in Visual Studio, and it's inside this class where our service logic will live. So this is the code that actually executes when our service is running. Now we have a class that contains our service logic, we're going to use Topshelf's fluent API to hook up events in the service lifecycle. So for example we'll say which piece of code in our class gets executed when our service starts. Finally now we've got our class containing our service logic and we've configured it using Topshelf's fluent API, we're actually going to run it in Visual Studio and see how we can execute and debug the service logic locally without needing to actually install it into Windows every time we make a change that we want to test.

Deploying, Debugging, and Logging Windows Services
Hi, welcome back. In the previous module we went and created our first Topshelf service. So we created a service class, and then we used the fluent interface to configure it. We then went ahead and tested the service logic locally without installing it to Windows. In this module we're going to be taking our service and actually deploy it to Windows, so we're going to use the Topshelf command line to actually install the service into Windows. So in this module we're going to start off by seeing how we can install our service into Windows. To do this we're going to open up a command-line prompt and navigate to the bin folder of our application, and then we're going to use the in-built Topshelf command line to actually go ahead and take our service and install it into Windows. We'll then go and use the Services snap-in to verify that our service has been installed. Now our service has been installed and we've started it up, we can then go and attach the Visual Studio debugger to the installed and running service. We can then set breakpoints and debug our service execution. Finally, we'll see how we can add additional logging to our service so we can obtain additional information when our service is running in production.

Managing Service Lifetime and Controlling Running Services
Hi, welcome back to this final module. In this module we're going to look at some additional techniques that are going to help us manage the service lifetime, and also allow us to control installed and running services. So in this module we're going to kick off by getting an overview of some of the additional features that we can make use of in our Windows services. We'll learn how we can enable the pausing and continuation of our installed Windows service, so when we enable this we can go into the Services snap-in and click the Pause and Continue buttons for our service. We'll learn about automatic service recovery, so we'll learn how we can configure our service to automatically restart if it fails, and we'll see how we can provide multiple recovery options as fallbacks. In addition to taking advantage of Windows services built-in pause and continue infrastructure, we can also define custom commands that our service can respond to. So we can write some code in our service class that gets executed when our service receives a specific custom command number, and we'll learn in the demo how we can use the sc command-line tool to actually send custom commands to our installed service. We're going to wrap up this module by getting an overview of some of the other features of Windows services that we can make use of in Topshelf. So for example we'll learn about different lifecycle hooks that we can take advantage of, such as executing code before installation of our service, and also specifying additional service dependencies such as specifying that our service depends on Microsoft Message Queuing being installed, for example.