Developing a Windows Service, Part IV: Debug the service
Programming, Windows April 8th, 2008Introduction
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.




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


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:
Related post
- Developing a Windows Service, Part II: Create a simple Windows Service Introduction In this part, I’ll show how to create a simple Windows Service project using VB.NET on Microsoft Visual Studio...
- Developing a Windows Service, Part III: Create a setup wizard Introduction In this part, I’m going to create an installer project for install my windows service ‘MyService’. The installer wizard...
- Developing a Windows Service, Part I: Introduction Introduction What is Windows service? Windows Service is an application or a program that can run without needing any user...
Related posts:





April 8th, 2009 at 7:56 am
Great!!! nicely explained … though there are many … but I saw this one to be good too ..
http://support.microsoft.com/kb/317421/en-us
and this :
http://mycomponent.blogspot.com/2009/04/create-debug-install-windows-service-in.html