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.

About these ads

18 Responses to “Windows Services in C#: Debugging Windows Services (part 4)”

  1. Sudeep Kaul Says:

    “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. Sudeep Kaul Says:

    Sorry forgot to add – I am running it under “Administrative” privilages.

  3. Sudeep Kaul Says:

    Hey !!! Got the solution it was UAC. Thanks anyways.

  4. John Baughman Says:

    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!

  5. ruben Says:

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

  6. PeteM Says:

    Ruben, this post explains how you can do it:

    http://weblogs.asp.net/paulballard/archive/2005/07/12/419175.aspx

    // The main entry point for the process
    static void Main()
    {
    #if DEBUG
    System.Diagnostics.Debugger.Launch();
    #endif …

    Also, this post takes it a bit further:

    http://theimes.com/archive/2006/12/28/Debugging-Windows-Services-is-a-Pain.aspx

  7. Eric Says:

    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!

  8. Cian Says:

    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!

  9. Florian Says:

    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.

  10. Mark Says:

    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

  11. Fahad Says:

    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

  12. Susan Says:

    I got this help from microsoft support. But yours is a detailed one. Thanks in Advance.

  13. Ashish Says:

    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?

  14. Mark Says:

    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.

  15. tosinrefind@gmail.com Says:

    i did not see “attach to proccess”

  16. Karen Austrin Says:

    Good guide. I’m still getting a headache from this. Man I wish debugging was easier

  17. Debugging a Windows Service application without installation « candor developer Says:

    […] For an old school windows services debugging comparison… Windows Services in C#: Debugging Windows Services (Part 4) http://arcanecode.com/2007/05/24/windows-services-in-c-debugging-windows-services-part-4/ […]

  18. mlang74 Says:

    You can debug windows services by turning them into a windows forms application to run locally, then back to a windows service for the real deployment. All this using conditional compilation. See the details on my blog.

    http://candordeveloper.com/2012/12/27/debugging-a-windows-service-application-without-install/


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 106 other followers

%d bloggers like this: