Tuesday, December 26, 2006

How to debug .net services ?

You wrote new windows service in .Net and you want to debug or run it from within the Visual Studio environment (just as you do when debugging console app) ? You want to debug a service that has been already installed and runs through the SCM(Service Control Manager)?

In this post I will cover two methods to debug a service developed in .Net. Each has it purpose.

Method 1:
After searching the web a bit and found no good way to debug a .Net service in a natural way, I did what I was familiar with since the Visual Studio 6 days - Add main() to the project.
Just add the following code to the service class:
static void Main(string[] args)
ServiceBase[] servicesToRun;
servicesToRun = new ServiceBase[] { new Service1() };
Now just put a break point on that method and hit F5.

Method 2:
Microsoft's way (described in the MSDN) to debug a service is to add a Sleep(30) in the OnStart() method of the service class and attach the debugger within these 30 secs. This is a problem - every time we start that service we'll need to wait those 30 sec! They sovles it by telling you to create 2 compiled versions of the same service. One with Sleep and the other without it. So why should we use this way if we can debug from the Visual Studio? A simple reason is when you experience different behavior when the service runs as a real service (through the SCM).
What if we could tell the service when we want it to wait 30 secs when it starts? This is a very good example for the use of the args that are in the OnStart(string[] args) .
Add the following to the beginning of the OnStart method:

for (int i = 0; i < args.Length; i++)
if (args[i].ToLower() == "/debug")
System.Threading.Thread.Sleep(30 * 1000);

Now after the service has been installed, run services.msc and double click the service.
debug windows .Net service
On the Start parameters field just write "/debug" and hit start.
Now you have a single project that allows you to attach to it's OnStart whenever you need.


No comments:

Post a Comment