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.


Monday, November 20, 2006

First post

Hey my name is Shmuel Krakover and I'm a System Programmer. For the last couple of years I've encountered an enormous number of issues all the way from
coding issues (ASP, JSP, C++, .NET ...)
to Application/Web servers issues (IIS, OAS, MTS)
and recently to OS related issues (Windows).

In my world there is no problem that cannot be solved.
Actually, any problem must be solved and even more, the cause for it must be completely understood. This means that I get allot reading architectures of platforms, whether its about MDAC, IIS or even as low as the OS itself.

Those technologies will probably change with time, so I'll have to deal with new problems in new areas and I'm about to share them because I'm absolutely sure that those problems are not mine only.

I hope this blog will be usefull for everyone.