How do I debug Windows services in Visual Studio?
You can also try this.
- Create your Windows service and install and start…. That is, Windows services must be running in your system.
- While your service is running, go to the Debug menu, click on Attach Process (or process in old Visual Studio)
- Find your running service, and then make sure the Show process from all users and Show processes in all sessions is selected, if not then select it.
- Click the Attach button
- Click OK
- Click Close
- Set a break point to your desirable location and wait for execute. It will debug automatic whenever your code reaches to that point.
- Remember, put your breakpoint at reachable place, if it is onStart(), then stop and start the service again
(After a lot of googling, I found this in "How to debug the Windows Services in Visual Studio".)
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