Introduction
A lot of workarounds exist to debug the OnStart
method of a service. A straight way with Debugger.Launch()
is described in How to Debug a Windows Service. But what if this wouldn't work, e.g. on Windows Vista x64? This sample shows a simple method to attach the debugger to the service process and debug the OnStart
method.
How It Works
The service process should wait till you have attached the debugger. The service process should not timeout. Just look at the code, it's quite easy.
Using the Code
All you have to do, is to copy this code snippet in your Onstart
method of your service class (derived from ServiceBase
).
Using Directives
using System.Diagnostics;
using System.Threading;
OnStart Method
protected override void OnStart(string[] args)
{
#if DEBUG
while (!Debugger.IsAttached)
{
RequestAdditionalTime(1000);
Thread.Sleep(1000);
}
RequestAdditionalTime(20000);
#endif
}
Start the service as usual. The service will wait until you attach the debugger to the service process. You can set breakpoints in your start code and debug it.
History
- 12th September, 2007: Initial version tested on Vista x64 / VS2005 with a x64 compiled C# project