Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Best Way to Debug a Windows Service program in Visual Studio 2008

I am using the Microsoft Log Parser in a Windows Service. The program works in a regular web page but when I put the code in a windows service it does not work. I put Breakponts in the windows service but when I run the code the program does not stop at the breakpoint. So through out my troubleshooting I have narrowed the problem down to the Log Parser software and Linq. So either Linq or the log parser software is creating the problem. Do you guys have any idea?

like image 532
Luke101 Avatar asked Oct 13 '09 21:10

Luke101


People also ask

How do I debug Windows Service application in Visual Studio?

Start Visual Studio with administrative credentials so you can attach to system processes. (Optional) On the Visual Studio menu bar, choose Tools, Options. In the Options dialog box, choose Debugging, Symbols, select the Microsoft Symbol Servers check box, and then choose the OK button.

How do I debug a Windows service without installing?

To debug without installing, add the code as follow, mainly #if (! DEBUG). You will be able to step through the code when you run the Windows Service without installing.


4 Answers

You need to attach your debugger directly with Windows Services. This might help you: http://msdn.microsoft.com/en-us/library/7a50syb3%28VS.80%29.aspx.

like image 182
Justin R. Avatar answered Nov 14 '22 23:11

Justin R.


I have done this many ways in the past depending on how the program runs. I think the easiest way is done with a if #DEBUG preprocessor directive around the Debugger.Launch() that way when you've built the project optimized the Debugger.Launch() call won't be compiled into the assembly.

One way we have also done done this task is with System.Windows.Forms.MessageBox.Show("attach") which allowed us to manually attach to the debugger whenever the "attach" dialog was displayed.

The last way which I do not prefer is to change the behavior of your service based on commandline params passed in. Basically opting NOT to start the services using ServiceBase.Run whenever a particular param was fired off, but calling a class that encapsulates the behavior/main function of the service.

like image 23
Wil P Avatar answered Nov 14 '22 23:11

Wil P


Do you want to debug the OnStart method? If so you can use System.Diagnostics.Debugger.Launch() or System.Diagnostics.Debugger.Break() method to get a chance to attach the debugger while the on start method is running, otherwise you're always to late with attaching the debugger.

like image 37
Chris Richner Avatar answered Nov 14 '22 23:11

Chris Richner


I think fat cat's suggestion of attaching your debugger to the service process sounds right. If that still doesn't work, try using Debug.WriteLine and DebugView.

like image 25
Don Kirkby Avatar answered Nov 14 '22 21:11

Don Kirkby