Intro to Windows Services in C#
Windows services are powerful tools in the .NET ecosystem, capable of performing background tasks without user intervention. Creating a Windows service in C# can sound complex, but in his video "Intro to Windows Services in C# - How to create, install, and use a service using Topshelf," Tim Corey breaks it down step-by-step.
In this article, we’ll walk through that video, covering each topic to provide clarity and continuity.
Introduction to Windows Services
Tim introduces Windows services as powerful, often-overlooked tools for automation. These applications are designed to run in the background, handling tasks without needing a user interface. Common examples include the SQL Server Agent or Hyper-V services.
Tim emphasizes the importance of practice for mastering C#. He recommends downloading the source code linked in the video description for hands-on learning.
Creating a Console Demo Application
Using Visual Studio, Tim creates a new console application project called SimpleHeartbeatService. Although you could use a Windows Service project template, Tim prefers the console approach for debugging simplicity.
"The idea here is not about what code I put in here, but instead on how to create the service itself," Tim notes.
What is a Windows Service?
Tim opens the Services tab in Windows using the Start menu search (just type "services" and press Enter). He explains that services running in the background perform vital operating system functions like remote desktop or MySQL server.
The goal is to create a new entry in this list using custom code.
Visual Studio Project Types: Console App vs. Windows Service
You might be tempted to select the Windows Service project in Visual Studio, but Tim explains it's harder to debug. Instead, a console app lets you run and test it locally, reducing the need to attach a debugger to a live service.
"The built-in service type project makes debugging harder because you have to install it first, then attach a debugger."
Adding Topshelf via NuGet
To simplify the service implementation, Tim adds Topshelf, an open-source framework, via NuGet. This popular library (with millions of downloads) helps create Windows services without boilerplate code.
Steps:
Right-click on References
Select Manage NuGet Packages
- Search and install Topshelf
Writing the Service Logic
Tim creates a Heartbeat class with two public methods: Start() and Stop(). Inside the constructor, he sets up a System.Timers.Timer to execute every second.
This timer's Elapsed event appends the current time to a .txt file in the C:\temp\demos\ folder. This mimics a background task that runs continuously.
"This timer will write to a text file every second, showing that the service is alive."
This simulates how long running processes work in real services, like folder scanning or printing documents.
Wiring Service with Topshelf
Using HostFactory.Run, Tim integrates Topshelf into the application:
Defines the Heartbeat class
Maps the Start() and Stop() methods
- Configures the service to run as LocalSystem, granting necessary permissions
He also sets metadata for the service:
Service name: HeartbeatService
Display name: Heartbeat Service
- Description: Used for demonstration purposes in a YouTube video
Running the Service as Console App
Before installation, Tim runs the application to test it. The timer correctly appends timestamps to heartbeat.txt. Using Notepad++'s Monitoring feature, he shows real-time updates.
Installing and Verifying the Service
Tim copies the .exe and related files from the bin\Debug folder to a permanent location (like C:\demos\HeartbeatService). Using an administrative command prompt, he runs the following commands:
SimpleHeartbeatService.exe install
SimpleHeartbeatService.exe start
SimpleHeartbeatService.exe install
SimpleHeartbeatService.exe start
This adds the service to the Service Control Manager. Opening the Services tab again and clicking Refresh, the service is now visible and running.
"It’s really easy to install a service, and it’s really easy to do things with a service."
To uninstall:
SimpleHeartbeatService.exe uninstall
SimpleHeartbeatService.exe uninstall
Recap: Benefits and Considerations
Tim recaps that creating and installing a service using Topshelf is straightforward. However, he warns that infinite file writing can lead to storage issues, which is a good reminder for proper log management.
Real-World Service Ideas
Tim shares practical ideas for Windows services:
Folder cleanup: Delete old files from Downloads
Image organization: Sort photos into folders by date
CSV processing: Watch a folder and auto-import CSVs into a database
- Email automation: Monitor a helpdesk table and send notifications
These examples show how to use services to automate background tasks and integrate with tools like SQL Server, OneDrive, and task schedulers.
Final Thoughts
Tim concludes by encouraging developers to experiment. Just be careful with memory, storage, and event logging. If you need auto update, security settings, or more robust deployment, further configuration will be required.
"There’s a lot of power here, and a lot of stuff you can do."
Conclusion
Using Tim Corey’s step-by-step demonstration in his video, creating a Windows service in C# becomes accessible and practical. By leveraging Topshelf, a console application can be converted into a fully functioning Windows service with minimal setup. Whether you’re managing log files, handling database events, or performing automation, services offer a powerful and scalable way to build real-time, background .NET Framework or .NET Core applications.
Use this tutorial as your full path to understanding, creating, and installing services efficiently. Don’t forget to test thoroughly and always run the installer or command prompt as an administrator.