Debug Windows Service in Visual Studio .NET
Windows services don't actually run directly within the IDE of Visual Studio .NET. There are however a few possibilities to debug them. You can for example install and start the Windows Service and attach a debugger to it. But there's another very straightforward solution ...
When you create a new Windows Service Project in Visual Studio (C#), you get the following main method (main entry point for the process) :
The magic is to use a conditional directive (#if (DEBUG)). Just set up your service, call your initialization method and let the main method sleep infinitively. Start (F5) to fire up your service and stop to kill it - like other runnable projects. It's that simple for debugging your Windows Service! Everything will still work in release as before without changing your code.
When you create a new Windows Service Project in Visual Studio (C#), you get the following main method (main entry point for the process) :
The magic is to use a conditional directive (#if (DEBUG)). Just set up your service, call your initialization method and let the main method sleep infinitively. Start (F5) to fire up your service and stop to kill it - like other runnable projects. It's that simple for debugging your Windows Service! Everything will still work in release as before without changing your code.
4 Comments:
At 2:13 AM, Anonymous said…
neat tip! we've been using a separate console app to get around this... and it's been quite an annoyance.
hey there is a tool called 'CopySourceAsHtml' that lets you copy/paste your code from Visual Studio into a blog entry as html with correct syntax highlighting (instead of creating an image)
cheers
At 10:21 AM, Norman said…
I created a ServiceRunner console app that you execute from the command line and tell it the full class path to the service you want to run. It uses reflection to create and run the service (so no accessor changes or extra methods required) It's about 50 lines, and makes debugging soooo much easier.
At 4:32 PM, Anonymous said…
Try this as a debugging solution. Let me know what you think:
http://theimes.com/archive/2006/12/28/Debugging-Windows-Services-is-a-Pain.aspx
At 2:44 AM, Anonymous said…
@ Norman :
Is this what you have done ??
http://mycomponent.blogspot.com/2009/04/create-debug-install-windows-service-in.html
Post a Comment
<< Home