Developing Windows Service on VB.NET, Part 4: Debug Service

This entry is part 4 of 4 in the series Developing Windows Service on VB.NET

From previous part, you’ve learned to develop a simple windows service on Microsoft Visual Studio 2005 and how to create a setup wizard for install the service. But it wouldn’t be complete, if you don’t know how to debug a windows service while you’re developing a windows service. This part is about how to debug a windows service.

Debugging a windows service isn’t the same as debug application that you simply press F5 or select Debug -> Start Debugging in MS Visual Studio 2005 bacause a windows service must be run in the context of the Services Control Manager (services.msc) not in MS Visual Studio.

To debug a windows service, you have to start the service in Services Control Manager and then attach a debugger to that process. But doing this way has some drawbacks that you can’t debug code in OnStart() method and Main() method since you have to start the service before you can attach a debugger to the service. There may be other workarounds but I wouldn’t write about that. I think this solution is sufficient if you put most of the code in other method rather than OnStart() and Main().

Step-by-step to debug service

  1. I’ll continue from previous part that I have installed ‘MyService’ as a service and already started it.
    Start MyService
  2. Back to MS Visual Studio 2005. Open ‘MyService’ project.
    Open MyService project
  3. Place a breakpoint to debug this line.
    Debug Service - Set breakpoint for debugging
  4. Click Debug -> Attach to Process…
    Select Attach to process
  5. Attach to Process window appears, you need to select the service to attach to.
    • If you haven’t start the service with user account, you have to check ‘Show processes from all users’ to see the service.
    • Select on the service ‘MyService’.
    • Click Attach.

    Attach a debugger to MyService service

  6. Wait a while until the service has executed to the breakpoint line and the service will be suspended waiting for you to debug.
    Step into breakpoint
  7. Now you can decide to continue or stop debugging as the same in normal application.
    Operation on debugging

Summary

This topic contains 4 parts about developing a windows service application on MS Visual Studio 2005 start from introduction to a windows service, code a windows service, install a windows service and debug a windows service. If you follow all of these parts, I’m surely that you can develop a windows service application on your own.

For more information, visit following links:

Series Navigation<< Developing Windows Service on VB.NET, Part 3: Create Setup Wizard

One Response

  1. Javs April 8, 2009

Leave a Reply