pavanarya – Pavan Kumar Aryasomayajulu

Contact Email : pavan.aryasomayajulu@gmail.com

Archive for February 2012

Debugging a Windows Service

with 4 comments

Hi in my previous post we saw about windows service and installing windows services in the windows service manager.

After installing the service and running the service if we try to debug from visual studio by keeping some break points and pressing F5 then for sure we will get a message when we keep our mouse pointer on the break point and the message is “Unable to load the symbols”.

This is because the service that we developed and installed in the windows service control manager runs in the context of the service control manager but not under visual studio or IIS.

So debugging the windows service is not a straight forward task. In order to debug it we should follow these steps.

Step1:
After developing the service we should install it in service control manager using the command “installutil”.

Step2:
Check whether the service is added in the service control manager or not. If it is present click on Start.

Step3:
Now the service is running. Now go to visual studio and click on attach process present in the debug menu and from the list add the service that we added using installutil command.

Step4:
Make sure we are adding the right process to debug. Because if we add some system process like “WindowsLogon” and we click on end then that particular process will end which results the system to hang.

Step5:
If we are unable to see the process in the step 3 make sure that it is added correctly or else click on “Show system Process”. Also check if we are having admin rights or not.

Step6:
Now the process is added and we can debug the code we want by keeping break points at appropriate places.

Now in order to debug the methods like onPause(), onContinue(), onStop() we should operate the process from the servcie control manager.

Now go to service control manager and find the process that we added. Now right click on the process and click on pause then immediately our function onPause() will be called and the break point will hit if we have it inside that method onPause(). The same is the case with onContinue() and onStop().

But there are few code pieces that cannot be debugged if we follow this process and let us discuss about them.

1. We cannot debug the code present in side the onStart() method because we are debugging a process that is already started inside the Service control manager and that particular function can be called only at the time of starting the process.

2. We cannot debug the main() method present in the code that is the entry point of the process execution because even this is executed at the time of starting the process.

Debugging the windows service using Debugger.break()

So don’t we have any process to debug the above mentioned two cases. What we are supposed to do if we have an issue in the onStart() or main() method
Yes there is a solution. Let us see that.
Add the name space System.Diagnostics to the service file. Now at the place where we want to set a debug point we can add the line Debugger.break().

Suppose if w e want to debug the onStart method() just add the namespace System.Diagnostics and then add the line Debugger.break() in the onStart method. Now build the solution and install the service.

Now right click on the service that we added and click on the start from inside the service control manager.

Now a pop up will open and it will ask us to select a visual studio version and after that if we click on ok then our windows service code will open and the breakpoint will stop at the line Debugger.Break(). Now we can add break point at places where ever we want in the code and can debug them without any problem

protected override void OnStart(string[] args)
       {
           Debugger.Break();
           timer.Enabled = true;
           timer.Interval = 10000;
           timer.Elapsed += new    System.Timers.ElapsedEventHandler(timer_Elapsed);
       }

With this method we can prevent the overhead of attaching the process.

Note: we should make sure that the line Debugger.Break() is commented or removed from the code that is being deployed to the production environment.

Written by pavanarya

February 5, 2012 at 11:05 pm

Posted in c#

Tagged with