Press enter to see results or esc to cancel.

Debugging Windows Services in Visual Studio

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someonePin on PinterestShare on Tumblr

Debugging Windows Services in Visual Studio while developing them can be frustrating.

Sometimes you test the Windows Service by building and installing it and then when trying a new method / function you need to stop the Windows Service, build it and start it again and for you to then debug it, you need to attach Visual Studio to the service process. But this is a time consuming process and makes me hate developing Windows Services…

But wait…

there is a very simple solution to our problem.

In your Program.cs there is most probably some code similar to this

When running the Windows Service in debug with this code, Visual Studio will tell you that it won’t work debugging the code and the debugger stops.

And here comes the trick…

Debugging Windows Services with Visual Studio

This code, whenever you set the execution mode to Debug, the service instance will be created and not be killed., and because you are not letting it be run by the ServiceProcess it won’t be recognized as a Windows Service, but as a common class letting you debug most of the Windows Service aspects.

This is actually one of my favorite code snippets I very often use. I have seen other ways by triggering a Windows Form and so on, but the Windows Form stuff requires you to add another reference, which is not necessary by doing it the way used in the above code snippet.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someonePin on PinterestShare on Tumblr
Comments

Leave a Comment