A Debuggable, Self-Installing Windows Service Template based on James Michael Hare's excellent blogposts:
- C# Toolbox: A Debuggable, Self-Installing Windows Service Template (1 of 2)
- C# Toolbox: A Debuggable, Self-Installing Windows Service Template (2 of 2)
- Download the Visual Studio template
- Create a new project and select / search for
DebuggableService
(see screenshot below) - Implement any of the provided
OnStart()
,OnStop()
,OnPause()
,OnContinue()
,OnShutDown()
andOnCustomCommand()
methods - Run/debug your application as usual, continue your development cycle as always
- When your application is done, open a console (with Administrative privileges!) and execute
MyApp.exe -install
to install your service; now you can start/stop/pause your service. To uninstall your service simply runMyApp.exe -uninstall
.
The WindowsService.cs
file contains the ServiceImplementation
class which, as it's name suggests, implements all service methods. This class has been fitted with an WindowsServiceAttribute
which you can use to control some service-specific properties like Name
, DisplayName
, Description
, UserName
, Password
, EventLogSource
, StartMode
, CanPauseAndContinue
, CanShutdown
, CanStop
and services your service depends on via ServiceDependsOn
.
Sure! No problem. I can recommend the excellent Topshelf project, also available as NuGet package. This allows for even more control, has way better documentation and is also available on GitHub!
Works (tested) with:
- Visual Studio 2013
- Visual Studio 2015