| Developing a Windows Service, Part IV: Debug the service |
Introduction
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
- I’ll continue from previous part that I have installed ‘MyService’ as a service and already started it.

- Back to MS Visual Studio 2005. Open ‘MyService’ project.

- Place a breakpoint to debug this line.

- Click Debug -> Attach to Process…

- 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.
- Wait a while until the service has executed to the breakpoint line and the service will be suspended waiting for you to debug.

- Now you can decide to continue or stop debugging as the same in normal application.

Summary



- 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.


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:
- MSDN - Introduction to Windows Service Applications
- MSDN - How to: Debug Windows Service Applications
- CodeProject - Developing a Windows SERVICE Application using .NET Framework with C#
- O’Reilly Windows DEV Center - Debugging Windows Services
Related post
- Developing a Windows Service, Part III: Create a setup wizard
- Developing a Windows Service, Part I: Introduction
- Developing a Windows Service, Part II: Create a simple Windows Service
- How to run command-line or execute external application from Java
- Getting started with Microsoft ISA Server 2006, Part I: Installation


























