Three little tips for debugging .Net Windows Services

I’m sure you’ve all read the MSDN article on debugging services, but I figured I’d share three quick tips that you might not think of. In no particular order:

  1. Start by building a console application. That will make the initial coding and debugging much easier as you won’t have to worry about installing the service or using the service control manager. Once you’ve got the console application working, you can make it into a service very easily. MSDN has a pretty good article on the steps involved, so I won’t repeat them. I like to have the option of debugging my service in console mode throughout the development process, so I add a call to the OnStart method to my Main. Using compiler directives lets me isolate the portions of the code which are specific to console or service execution. The final product looks something like:
     #define CONSOLE  static void Main(string[] args) {     using(MainService theService = new MainService())   {       //set up the service #if CONSOLE        theService.OnStart(null); #else                     ServiceBase.Run(theService); #endif     } }  

    If I want to switch to service mode, I just comment out the declaration directive.

     //#define CONSOLE 

    Using this technique let’s me get most of the way towards my service functionality without having to open the Service Control Manager (SCM) once.

  2. At some point you’re still going to need to debug the service as a service. Don’t, I repeat, don’t do what I did and listen to the MSDN article I mentioned earlier when it comes to launching the debugger. You’ll drive yourself nuts hitting start in the SCM, and then switching over to VS.NET to attach to the process before the line of code you want to test is hit. Especially if you’re trying to debug the OnStart method. Instead, add a simple yet beautiful line of code to the service:

    Bam! No more rushing. Go ahead and start your service in the SCM and the “would you like to debug” dialog will pop up as soon as that line is reached. If I had realized that earlier I might have avoided a nasty finger sprain ;).

  3. Once you’re in the home stretch you’re going to have some bugs to squash. Well, not you, but the other non-perfect devs on your team. Nothing is more annoying than having to stop the darn service every time you need to do a build. Especially since I always forget and wind up getting a bunch of compilation errors about not being able to write to the file. Instead, in VS.NET choose the “Projects” menu and click properties. Under the “Common Properties” folder you’ll find the Build Events node. Click on that and enter the following code into the “Pre-build Event Command Line” entry box. Now, whenever you do a build the service will be stopped and your build will suceed. Well, if your code is compilable that is.
     net stop "MOM Logger Service"
    if errorlevel 1 echo "Service wasn't running, ignoring."
    if errorlevel 1 goto CSharpEnd

    I don’t claim to be a great batch scripter, but this works.

Ok, that’s it. Nothing mindblowing, I know, but this isn’t .Net Notes. Enjoy!