Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,
I have to set up an debugging environment for an Windows application which calls an Windows service. I am not able to debug windows service. Actually I don't have service in my Services folder (Services.msc). How to create manually?
Can anyone help me to solve this?


Thanks
Antoni
Posted
Updated 14-Apr-14 7:14am
v2
Comments
ZurdoDev 14-Apr-14 13:17pm    
Do you have the code for the windows service?
Sergey Alexandrovich Kryukov 14-Apr-14 13:41pm    
The question about debugging is pretty interesting; I up-voted the question.
—SA

You don't need or can have any special environment, but debugging of a Windows Service is not so easy.

One thing you can do it just compiling it under Visual Studio in place of deployment, start it in a regular way using the Service Controller, and then, in Visual Studio, use "Debug -> Attach Process". The execution will stop at some breakpoints you could set in advance, and then you can continue step be step debugging. This is described here:
http://msdn.microsoft.com/en-us/library/7a50syb3%28v=vs.110%29.aspx[^].

This method is hard to debug the earlier steps of execution of a Windows Service process. To cover those steps, you can, say, create an artificial delays. Many things can be resolved by using logging. Please see: http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.eventlog[^].

I've used one much less usual and dedicated approach to debugging. I developed a special type of application which can work as both an interactive application and as the service, depending how it is hosted. If you start the application from the Shell, it is executed in an interactive mode, and if you start it from "Services" applet (by a Service Controller), it is executed as service. Important for debugging feature is: both modes use the same methods, which are designed to work in both environments. Moreover, for an interactive mode, I created a special console-like window (not a standard console, just based on some list box) to show logging information, by implementing dual logging mechanism (in service mode, it uses EventLog, see above).

For this technique, the key is the detection of the execution mode by such dual-mode application. This is done by using the static property System.Environment.UserInteractive:
http://msdn.microsoft.com/en-us/library/system.environment.userinteractive%28v=vs.110%29.aspx[^].

This approach helped me to debug nearly all of the problems using just the interactive mode. The problems which were manifested only in the service mode also existed; so I fixed them using just the logging. It's important to note that the applications I developed using this techniques were extremely complicated. If the Service is not extremely complex, you could walk out doing nearly all debugging in the interactive mode.

[EDIT]

See also my past answer on my dual-mode Service application: Configure Windows Service with UI[^].

On the problem of the installation, see also: How to install windows service programmatically[^].

Please see also my other past answers on Windows Service creation and installation:
Configure Windows Service with UI[^],
Windows services Error[^],
http://msdn.microsoft.com/en-us/library/zt39148a.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Abhinav S 14-Apr-14 13:45pm    
5!
Sergey Alexandrovich Kryukov 14-Apr-14 14:16pm    
Thank you, Abhinav. I just added some more links to my past answers, to cover dual-mode approach and installation.
—SA
Abhinav S 14-Apr-14 14:21pm    
You are welcome.
The first step for you would be to create and install a service - http://support.microsoft.com/kb/251192[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Apr-14 13:40pm    
Main problem is the debugging, not creation and installation. The question is pretty interesting. Please see my answer.
—SA

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