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

4

5

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?

Schechinger answered 13/10, 2009 at 21:50 Comment(0)
F
2

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.

Fruitful answered 14/10, 2009 at 1:23 Comment(1)
Re the last point - I believe you can also check Environment.UserInteractive for this.Coopersmith
F
5

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.

Fogel answered 13/10, 2009 at 21:53 Comment(0)
L
2

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.

Lawtun answered 13/10, 2009 at 22:3 Comment(0)
A
2

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.

Anywhere answered 13/10, 2009 at 22:6 Comment(0)
F
2

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.

Fruitful answered 14/10, 2009 at 1:23 Comment(1)
Re the last point - I believe you can also check Environment.UserInteractive for this.Coopersmith

© 2022 - 2024 — McMap. All rights reserved.