Thursday, December 6, 2007

Debugging Windows Service without deploying it

Have you ever tried to build a Windows Service? Did you get it right the first time? Chances are that you had to modify it a couple of time before getting it the way you want it. Each time you had to build the installation package, uninstall the old version and reinstall the new one. Hopfully you can get the installation package to remove old version before installing new one. How did you debug it? Your only option was to start the service and attache a debugger to it. Although this is a good way to do it and you must go to that process before going into production with your service, this is way too much time consuming for the deveopment process. I invite you to take look at a simple class I built to enable any Windows services to be debug from Visual Studio using F5. Here are the step you have to follow:
  1. Build or open your own Windows Service project
  2. Change it's build setting to be a console application
  3. Add ServiceDebuggerHelper project to your solution (can be download from CodeProject)
  4. Add a reference to ServiceDebuggerHelper project in you project
  5. Modify your Program class to start your service with ServiceRunner if it is started with /debug command line argument
  6. Modify your service to be debuggable in one of two way:
    1. Implement IDebuggableService interface
    2. Inherit from DebuggableService base class
  7. Start it in debug mode

See Debugging Windows Service made easy on CodeProject for details.