Plunging into .NET Development

Weblog Pieter Gheysens
Microsoft .NET Development - C# - Enterprise Library - Visual Studio 2005 Team System - Compuware DevPartner - ...
 


Friday, June 23

Debug Windows Service in Visual Studio .NET

Windows services don't actually run directly within the IDE of Visual Studio .NET. There are however a few possibilities to debug them. You can for example install and start the Windows Service and attach a debugger to it. But there's another very straightforward solution ...

When you create a new Windows Service Project in Visual Studio (C#), you get the following main method (main entry point for the process) :



The magic is to use a conditional directive (#if (DEBUG)). Just set up your service, call your initialization method and let the main method sleep infinitively. Start (F5) to fire up your service and stop to kill it - like other runnable projects. It's that simple for debugging your Windows Service! Everything will still work in release as before without changing your code.


6 Comments:

  • At 2:13 AM, Anonymous Anonymous said…

    neat tip! we've been using a separate console app to get around this... and it's been quite an annoyance.

    hey there is a tool called 'CopySourceAsHtml' that lets you copy/paste your code from Visual Studio into a blog entry as html with correct syntax highlighting (instead of creating an image)

    cheers

     
  • At 8:08 AM, Blogger Awais Bin Khalid said…

    Well, you do need to change the access specifier for OnStart & OnStop method. They are protected by default and can't be called from Program Class, once you make them public, this would work fine. You would need to change them for public release though.

    Nice trick though!

     
  • At 8:31 AM, Blogger Awais Bin Khalid said…

    nice idea.

    But it's not that seamleass, the access specifier for OnStart function is protected, so you need to create a method that calls it.

    Thanks again, for saving lots of my time.

     
  • At 10:21 AM, Blogger Norman Rasmussen said…

    I created a ServiceRunner console app that you execute from the command line and tell it the full class path to the service you want to run. It uses reflection to create and run the service (so no accessor changes or extra methods required) It's about 50 lines, and makes debugging soooo much easier.

     
  • At 4:32 PM, Anonymous Anderson Imes said…

    Try this as a debugging solution. Let me know what you think:
    http://theimes.com/archive/2006/12/28/Debugging-Windows-Services-is-a-Pain.aspx

     
  • At 2:44 AM, Anonymous Anonymous said…

Post a Comment

Links to this post:

Create a Link

<< Home