Windows Services in C#: Debugging Windows Services (part 4)

In part 1 of this series I mentioned debugging a windows service was a little different than normal debugging of an application. Today we’ll look into how you can debug your windows service.

First, open Visual Studio and have your project loaded, if it’s not already there. Now go over to the MMC (as I described in part 3) and make sure it’s logging events.

Now comes the neat part. Under the Debug menu in Visual Studio, select “Attach to process…”. When the dialog below appears, you will need to check the “Show processes from all users” and “Show processes in all sessions” boxes. Now your list should update correctly.

Scroll down and look for the process with the same name as your executable, in my case it was TimeLogger.exe. Click on it, and the click the “Attach” button in the lower left.

[Picture of Attach to Service Dialog]

If all went well Visual Studio should shift to “Run” mode. Your code will be locked (sorry, no edit continue with windows services). But you can go in and create breakpoints, as I’ve done here (click on the pic to see a larger version of it):

[Pic of VS ready to debug]

Now sit back and wait a minute, when our service fires the _timer_Elapsed event it will fall into the standard debug mode you’re used to, as you can see below.

[Pic of VS stepping thru the service]

In the screen above you can see where I took one step and am now on the line of code “string message =…”. I have access to my locals, as well as the call stack and other debugging tools. From here I can do the normal debug tasks, including stepping or just hitting F5 to continue.

When you are done debugging and are ready to disconnect from the service, simply return to the Debug menu and this time pick “Stop Debugging” (or hit Shift+F5). Visual Studio disconnects you from the running service and returns you to normal code editing mode.

Resetting for another test is still a bit painful. You’ll want to stop your service, then in your Visual Studio Command Prompt window run InstallUtil, this time with the /u option to uninstall it. (instalutil /u timelogger.exe). Then you can build, then reinstall your service.

I said this yesterday, but I want to stress it again. If you are developing under Vista, it is vitally important you run VS Command Prompt as the Administrator (simply right click on the menu option and pick run as administrator). If you don’t do this, instalutil will fail every time.

And that’s how you debug an windows service. It’s not really that difficult, now that you know the steps involved.

18 thoughts on “Windows Services in C#: Debugging Windows Services (part 4)

  1. “Unable to attch to the process. You do not have the permission to debug the process” – This is what I am getting while trying to debug the project in Windows Vista Home Premium and .Net 3.0 . What should I do ?

  2. Another option to the installutil utility, you can add auto install code and at a command prompt install and uninstall your service by running it with a simple parameter. The attach to process part is still the same. I can send you a sample project of how I did it so you could update if you like the idea. It’s a little quicker to run.

    The only drawback to debugging services is that you can’t easily debug a self starting service, and backing out of that code kind of hurts the ego… 😉

    Great stuff here though! Thanks!

  3. How do you debug the OnStart() Event? If your process has to be already running then it’ll never hit that event.

  4. I have the exact same problem as ruben …
    I’ve done all the stuff and attached properly to the service-process successfully and Visual Studio enters debug mode but it never breaks at any of my breakpoints, I’ve tried restarting the service and then re-attaching to the process but no luck:
    VS2005 never enters the breakpoints ! 😦

    Any ideas would be appriciated … I’ve created dosens of breakpoints all over the service but no luck!

  5. You could put a Sleep for 10 minutes at the start of your OnStart. Then put a break point on the next line.

    Now all you need to do is Restart the service and attach before ten minutes runs out.

    hackalicious I know!

  6. Thank you for your great howto – I appreciate it.

    My experience is that there is no need to uninstall and install again to make some changes.

    Just if you change the name of the service you should prior uninstall. If this is forgotten use the sc tool.

    Cheers.

  7. hi, nice article… i have a problem… i have a service running, but when try attaching to it, i get an error that a debugger is already assigned…. some one might have attached a debugger to it…but how do i continue with it… thanxx

  8. hit there

    a bit sort of same problem as discuses above near by above user’s .
    The Thing is .
    the service and all its ingredients a re installed .
    Using vista i have already disable UAC.
    using vs 2005 C#.
    i step in in to the debug mode but eventually when trying to step in or f10,f11 .
    i can able to step in into few lines of my code successfully but at some stage when i tried to stepped in for more lines my Vs 2005 goes out of the debug mode .

    so can any 1 help me in this or any suggestions.

    Regards

    And Thnx In Advance

  9. I use window service it work well with database and webservice.asmx but this cant send sms . This window sevice can send sms only in debug mode. Somebody have solution?

  10. Here’s my tip for debugging a service: Comment out the existing Main() function in Program.cs. Replace it with a command line style Main(string[] args) function. Into this new function, copy the contents of your OnStart() function (From Service.cs). Then add this line:

    System.Threading.Thread.CurrentThread.Join();

    Now you can run the service right from inside Visual Studio. Press the “play” button. That thread join line will cause your debug session to run forever. Click the stop button to stop it.

    Of course you can’t debug through your Service.cs file because you just bypassed it, but everything outside there is fair game for breakpoints.

Leave a comment