Nazmul weblog

"Winner's don't do different stuff , they do stuff in their own way"

How to debug a windows service?

 

Last few months I and my teammates are working with several service base applications. In last project I had faced great deal of problem with service. That was my first windows service application. That time I realized that windows service application does not allow debugging as normal application. That project I was using log file to debug the win service application. I have not enough time to find out that problem solution. Later I find a solution how to debug a win service application as like normal application (but not quite similar).

A windows service was a little different than normal debugging of an application. Here we’ll look into how you can debug your windows service.

First, open Visual Studio and load your win service application, if it’s not already there. Assuming your build was successful; you can now install and test your windows service. There are two ways to install, we can use the installutil.exe, or create a full blown MSI installer.

We’ll need to open a Visual Studio Command Window. Start, All Programs, Microsoft Visual Studio 2005, Visual Studio Tools, Visual Studio Command Window. If you are running under Vista, STOP! Do NOT click on Visual Studio Command Window. Instead, right click and pick “Run as Administrator”. Again that’s for Vista, for XP just click since you likely have Admin rights.

Now in the command window navigate to the bin\debug folder where your project compiled.



Now comes the neat part. Under the Debug menu in Visual Studio, select “Attach to process…” When the dialog below appears, you will need to check the “Show processes from all users” and “Show processes in all sessions” boxes. Now your list should update correctly.



Scroll down and look for the process with the same name as your executable, in my case it was TimeLogger.exe. Click on it and the click the “Attach” button in the lower left.

If all went well Visual Studio should shift to “Run” mode. Your code will be locked (sorry, no edit continue with windows services). But you can go in and create breakpoints.



Now sit back and wait a minute, when our service fired (Use service Manager for start or stop service). It will fall into the standard debug mode you’re used to, as you can see below.



When you are done debugging and are ready to disconnect from the service, simply return to the Debug menu and this time pick “Stop Debugging” (or hit Shift+F5). Visual Studio disconnects you from the running service and returns you to normal code editing mode.

Resetting for another test is still a bit painful. You’ll want to stop your service, then in your Visual Studio Command Prompt window run InstallUtil, this time with the /u option to uninstall it. (instalutil /u timelogger.exe). Then you can build, and then reinstall your service.



Reference


http://arcanecode.wordpress.com/

 

 

Comments

mamun said:

Thanks Nazmul. It will help me lot to walk through windows service.

# February 23, 2009 12:04 PM