Overview
Topshelf is a framework that simplifies creating and managing Windows services. Instead of building a traditional Windows service project, you can build a console application and let Topshelf handle the service hosting. This approach makes development and debugging significantly easier.
Official Resources
- Official Website: http://topshelf-project.com/
- Documentation: https://topshelf.readthedocs.io/en/latest/
- GitHub Repository: https://github.com/Topshelf/Topshelf
Implementation Guide
Step 1: Create a Console Application
Create a new console application project and install the Topshelf package via NuGet:
Install-Package Topshelf
Step 2: Create a Service Class
Create a class that implements the service logic. The class needs Start() and Stop() methods that Topshelf will call when the service starts or stops.
using System;
using System.Timers;
public class HelloService
{
private readonly Timer _timer;
public HelloService()
{
_timer = new Timer(1000) { AutoReset = true };
_timer.Elapsed += (sender, args) => Execute();
}
public void Start()
{
_timer.Start();
}
public void Stop()
{
_timer.Stop();
}
private static void Execute()
{
Console.WriteLine("Hello from Topshelf service");
}
}
Step 3: Configure the Host in Main
Modify your Program.cs to configure Topshelf as the service host:
using Topshelf;
namespace HelloService
{
class Program
{
static void Main(string[] args)
{
HostFactory.Run(x =>
{
x.RunAsLocalSystem();
x.SetDescription("Sample Topshelf Windows Service");
x.SetDisplayName("HelloService");
x.SetServiceName("HelloService");
x.Service<HelloService>(s =>
{
s.ConstructUsing(name => new HelloService());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
});
}
}
}
Step 4: Run the Application
Execute the console application. You should see the message "Hello from Topshelf service" printed every second.
Service Management Commands
Navigate to the output directory and run the folowing commands with administrator privileges:
| Action | Command |
|---|---|
| Install | HelloService.exe install |
| Start | HelloService.exe start |
| Stop | HelloService.exe stop |
| Uninstall | HelloService.exe uninstall |
After installation, the service will appear in the Windows Services management console.
Complete Example Code
using System;
using System.Timers;
using Topshelf;
namespace HelloService
{
class Program
{
static void Main(string[] args)
{
HostFactory.Run(x =>
{
x.RunAsLocalSystem();
x.SetDescription("Sample Topshelf Windows Service");
x.SetDisplayName("HelloService");
x.SetServiceName("HelloService");
x.Service<HelloService>(s =>
{
s.ConstructUsing(name => new HelloService());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
});
}
}
public class HelloService
{
private readonly Timer _timer;
public HelloService()
{
_timer = new Timer(1000) { AutoReset = true };
_timer.Elapsed += (sender, args) => Execute();
}
public void Start()
{
_timer.Start();
}
public void Stop()
{
_timer.Stop();
}
private static void Execute()
{
Console.WriteLine("Hello from Topshelf service");
}
}
}