.NET Tip of The Day
Learn one new .NET trick every day
Быстрое пополнение счета телефона      Login or Join

How to debug Windows Service startup

Actually you can use this trick every time when:

  1. You want to debug a process startup.
  2. You can't run the process by hitting F5 in Visual Studio (for example, Windows Service).

So the problem is that you can't simply attach the Visual Studio debugger to the process as there isn't one to attach to until after you start the process. However, once you start the process if you have a bug such as an exception in the process's initialization you won't get the debugger attached to the process before it's too late.

Calling Debugger.Launch() or Debugger.Break() in your code allow you to debug such problems. This methods pop up the following screen asking you which instance of the debugger it should use to debug the application.

JIT Debugger
4/16/2008

Comments:

To extend this trick, you can add a commandline switch (e.g. -debug) to you service program. (The program will only call Debugger.Break if the switch is set.) Then, if you want to debug you just modify ImagePath in the registry (HKLM/SYSTEM/CurrentControlSet/Services/YOURSERVICE) and append the switch.

Another problem is, that service control manager kills the process if it does not respond within a given timeout. To influence this timeout you have to edit the registry at
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control -> ServicesPipeTimeout

Much more interesting information can be read in this wonderful knowledge base article: http://support.microsoft.com/kb/824344

Regards,
a4711


a4711 4/18/2008 9:50:51 AM

Good remark about timeout. Sometimes it's hard to debug when you have less than 30 seconds ;)

kostya.ly 4/20/2008 12:41:53 AM

I had to do remote debugging(start up). ie service is installed in a machine where there is no visual studio installed. You know what I did ? :D.. When service starts it sleeps(Thread.Sleep()) for 1 minute. by then I would have connected my visual studio to the service .. simple !!

sanjay 4/7/2010 5:56:56 PM

Name
URL
E-mail
Provide your e-mail address to receive notification about new comments.
Message
HTML tags are not supported.
Please add 5 and 6 and type the answer here:
RSS .NET Tip of The Day
Subscribe to receive one tip from the .NET Tips and Tricks Community per day.
Previous Tips of The Day
The best of the .NET Tips & Tricks Community.
.NET Practitioners .NET Tips & Tricks Community
Every .NET practitioner has a trick up in their sleeve. This is the place to share it with other .NET people.
Submit a Tip
Discovered a new trick? Share it with others.
My Tips
Manage tips you authored.