Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to debug a Windows service but it pops an error message saying

Cannot start service from the command line or a debugger. A windows service must be installed using installutil.exe and then started with the Server explorer, windows services Administrative tools or the NET start command.
Posted

To debug a service add a delay in the OnStart method like Thread.Sleep(10000);.

Then you have to attach your debugger to that process http://msdn.microsoft.com/en-us/library/c6wf8e4z.aspx[^].
 
Share this answer
 
Comments
Nish Nishant 31-Oct-11 11:49am    
Voted 5. I endorse and recommend this answer. ;-)
Mehdi Gholam 31-Oct-11 11:57am    
Thanks
Sergey Alexandrovich Kryukov 31-Oct-11 13:16pm    
My 5, too, but I used more advanced programming which helped me with debugging a lot. Please see my solution.
--SA
Espen Harlinn 31-Oct-11 11:50am    
That's useful too :)
Mehdi Gholam 31-Oct-11 11:57am    
Cheers
Take a look at this page:
http://msdn.microsoft.com/en-us/library/50614e95(VS.80).aspx[^]

installutil lets you install, and uninstall services - they have to run under the service control manager[^].

I often find the convenient to test the implementation of a service using a win forms or command line process by separating the actual implementation from the windows service implementation.

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Nish Nishant 31-Oct-11 11:53am    
Voted 5. I endorse and recommend this answer. [I am going to use this as my approval template text from now on]
Espen Harlinn 31-Oct-11 12:06pm    
Thank you, Nish :)
Sergey Alexandrovich Kryukov 31-Oct-11 13:16pm    
My 5, too, but I used more advanced programming which helped me with debugging a lot. Please see my solution.
--SA
Mehdi Gholam 31-Oct-11 12:01pm    
There are time when you have to test under the exact conditions (mostly to do with the security settings of the SYSTEM account) and this requires the attachment of the debugger to the actual process.

My 5 anyway.
Espen Harlinn 31-Oct-11 12:08pm    
That's true, but usually you would want to run the service under a service account, and not the SYSTEM account - anyway, thanks Mehdi :)
I used alternative approach: developing of the application which can be run in both modes: as a windows service and as an interactive application. The application can detect during runtime how it is currently hosted and behave slightly differently. In interactive mode, it can do a lot more, for example, use UI to show logging immediately. The interactive mode makes most of the debugging way easier. It can also be used to install and uninstall the application as a service, start/stop/restart the service running in another instance of the process using Service Controller.

Please see my past solution: How to install windows service programmatically[^].

—SA
 
Share this answer
 
Comments
Mehdi Gholam 31-Oct-11 13:27pm    
I have a piece of code that can install itself as a service or run on the command line. Mysql does this also, I love programs like these as they make life easy. :)
5'ed
Sergey Alexandrovich Kryukov 31-Oct-11 13:34pm    
You cannot imaging how my approach helped debugging. We managed to debug manufacturing control systems, with all the hardware on my part with authors of close-source software and hardware installed on my site using just telephone and internet (with Web camera) on my machine. We successfully debugged it all with hardware, and I never met the engineers who worked with me as they worked in a different state and did not have time for a trip to our company. Did you even hear of such tricks? :-)

Thank you, Mehdi.
--SA
Espen Harlinn 31-Oct-11 15:18pm    
5'ed - Deriving from the service implementation from Component, and using variant of the Log4Net event logger, included with the Log4Net examples, is a nice setup.
Sergey Alexandrovich Kryukov 31-Oct-11 16:07pm    
Thank you, Espen.
Not that I understood your note about Log4Net logger (yes, it could be used, of course). I used a logger based on System.Event.Log, but it switched to some interactive logging on if UserInteractive == true.
--SA
I think this article will also be helpful for U
Debugging Windows Services under Visual Studio .NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900