How do I debug Windows services in Visual Studio?

PawanS picture PawanS · Jan 13, 2011 · Viewed 105.2k times · Source

Is it possible to debug the Windows services in Visual Studio?

I used code like

System.Diagnostics.Debugger.Break();

but it is giving some code error like:

I got two event error: eventID 4096 VsJITDebugger and "The service did not respond to the start or control request in a timely fashion."

Answer

Chirag picture Chirag · Mar 28, 2014

Use the following code in service OnStart method:

System.Diagnostics.Debugger.Launch();

Choose the Visual Studio option from the pop up message.

Note: To use it in only Debug mode, a #if DEBUG compiler directive can be used, as follows. This will prevent accidental or debugging in release mode on a production server.

#if DEBUG
    System.Diagnostics.Debugger.Launch();
#endif