How do you debug a windows service?

WellsCarrie

Well-known member
Joined
Jul 12, 2005
Messages
95
Location
Arkansas
Programming Experience
5-10
The line
System.Diagnostics.Debugger.Break()

is causing an uninteded loop to happen in my service. Any ideas on what I'm doing wrong?

Can anyone help me?

thanks
Carrie
 
Last edited:
Hi Carrie ,

You can use Debug Process Option from VS.Net or you can use a third party Debug tool such as Debug Viewer to Debug the Windows Service.

Just add System.Diagnostics.Debug.WriteLine("Message to Display in Debug View")
to the code where you want to Debug .


Thanks ,
Jay
 
Do you have the directions on how to do that (Debug Process Option)? I've done it once before, and can't remember where or how I did it.... it would have come in REALY handy about three weeks ago....

Tg
 
1) Add System.Diagnostics.Debug.Break() twice several lines above where you want to start debugging.

2) Compile your service.

3) Install your service.

4) Start your sercice. The service will run until it gets to the complied "break".

5) When it asks what debugger you want to use pick a "NEW" instance of the .NET IDE.

6) When it asks what you want to see Pick check boxes 1 and 3. (this will take you into the native code and not the assembly. Say OK

7) After a short (usually 30 to 45 seconds) pause you will be presented with your code and a GREEN highlight on your first encountered "break" line.

8) click the little green arrow for continue (for some reason on my machine pushing f5, f10, or f11 at the first "break" makes the debug crash) hince my suggestion to put in two break lines.

9) a normal yellow highlight should apear on you next "break" statement. From there you may set watches and move through your code with f10 and f11 as normal.

Happy Debugging

Oh a few "little" things I found when working this way: pushing the stop button or closing the debugger will stop your service. You will have to refreash your services window to see this though.

You must ALWAYS Uninstall and then INSTALL your service to restart the Debugging process. For some reason on my end even if I didn't make a change but just stoped and started the service I would get service admin errors (some times 3 to 5 pop ups) and the service would never start again.

I am sorry for an gramatical mistakes you may find here!
 
Eeeew.... Hmmm.... that looks a bit more complicated that what I had used previously... If I remember right, there was something about running it and then attaching to the process some how... I'll look to see if I can find it...

Tg
 
I knew there was an easier way. Load the project, start your service (not in the IDE though, but the actual service.) Set normal break points (F9) where ever you need them. Then under the Tools menu, select Debug Process. In the list, find your running service, and click the "Attach" button. The next time it hits one of the break points, it falls into the IDE, nicely, neatly, and safely.

Tg
 
If you do find a simpler way please post it!

I looked for 4 days before I found this one. I tried a 10 other "suggestions" that didn't work at all.

Carrie
 
If you look at my last post (#6) it does have the easier way. Attach to the process, and set some breakpoints. I forget where I found it exactly, I think it was on MSDN, and that's the way they suggest doing it.

Tg
 
Back
Top