Debugging a Windows Service Startup Method
It’s been a long time since I wrote a Windows service but…I was doing that today and realized that the more things change, the more they stay the same. It would seem that it’s still a painful and problematic process if you want to debug the startup of a custom Windows service you write in Visual Studio. If you follow the published guidance around this (https://msdn.microsoft.com/en-us/library/7a50syb3(v=vs.110).aspx), it suggests a variety of work-arounds for doing this, but nothing that I would call an out of the box solution.
Well…there IS an out of the box solution. So even though it may not be rocket science I guess it’s worth sharing. If you want to break in on the OnStart event of your custom Windows service, just add this line in your code where you want to start debugging: Debugger.Launch();
Compile your code then install the Windows service. When you start the service you should see a dialog that asks you if you want to debug the service – like this:
Just say yes and another dialog will pop up where it asks you if you want to start a new instance of Visual Studio, or use an existing instance. Of course you will have Visual Studio already up and going with your source code for your service loaded and break points set. Just pick that instance of Visual Studio and – voila’ – you are debugging OnStart. Old school sometimes works best.