Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug Windows services in Visual Studio?

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."

like image 832
PawanS Avatar asked Jan 13 '11 10:01

PawanS


People also ask

How do you Debug or test your Windows service without installing it in VB net?

msc and use Visual Studio > Debug > Attach to Process menu and attach to the Windows service. You can also consider using Thread. Sleep(10000) as the first line in the OnStart call, or Debugger. Break() to help you out to be able to attach before the service executes any work.


1 Answers

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 
like image 180
Chirag Avatar answered Sep 26 '22 18:09

Chirag