How to: Debug Windows Service Applications
Because a service must be run from within the context of the Services Control Manager rather than from within Visual Studio, debugging a service is not as straightforward as debugging other Visual Studio application types. To debug a service, you must start the service and then attach a debugger to the process in which it is running. You can then debug your application using all of the standard debugging functionality of Visual Studio.
Caution |
|---|
You should not attach to a process unless you know what the process is and understand the consequences of attaching to and possibly killing that process. For example, if you attach to the WinLogon process and then stop debugging, the system will halt because it cannot operate without WinLogon. |
You can only attach the debugger to a running service. The attachment process interrupts the current functioning of your service; it does not actually stop or pause the service's processing. That is, if your service is running when you begin debugging, it is still technically in the Started state as you debug it, but its processing has been suspended.
Attaching to the service's process allows you to debug most but not all of the service's code; for example, because the service has already been started, you cannot debug the code in the service's OnStart method this way, or the code in the Main method that is used to load the service. One way to work around this is to create a temporary second service in your service application that exists only to aid in debugging. You can install both services, and then start this "dummy" service to load the service process. Once the temporary service has started the process, you can then use the Debug menu in Visual Studio to attach to the service process.
After attaching to the process, you can set breakpoints and use these to debug your code. Once you exit the dialog box you use to attach to the process, you are effectively in debug mode. You can use the Services Control Manager to start, stop, pause and continue your service, thus hitting the breakpoints you've set. You would later remove this dummy service after debugging is successful.
Note |
|---|
Debugging the OnStart method can be difficult because the Windows Service Manager imposes a 30-second limit on all attempts to start a service. For more information, see Troubleshooting: Debugging Windows Services. |
To debug a service
Install your service. For more information, see How to: Install and Uninstall Services.
Start your service, either from Services Control Manager, Server Explorer, or from code. For more information, see How to: Start Services.
In Visual Studio, choose Attach to Process from the Debug menu.
The Processes dialog box appears.
Click Show system processes.
In the Available Processes section, click the process for your service, and then click Attach.
TipThe process will have the same name as the executable file for your service.
The Attach to Process dialog box appears.
Choose any appropriate options, and then click OK to close the dialog box.
NoteYou are now in debug mode.
Set any breakpoints you want to use in your code.
Access the Services Control Manager and manipulate your service, sending stop, pause, and continue commands to hit your breakpoints. For more information on running the Services Control Manager, see How to: Start Services.
- 4/23/2012
- cogaritis
- 2/15/2012
- pappasa
1. Run your Visual Studio as an Administrator with permissions,
2. Install your service and make it started,
3. On the 'Attach to Process' dialog, make both check boxes 'Show processes from all users' and 'Show processes in all sessions' checked,
Now you can see two of your service processes in the Available Processes list box:
- yourService.exe that is enabled and related to your service code
- yourService.vshost.exe, still disabled (grayed out) that is a hosting process to improve debugging performance, nothing to your service logic
Simply select yourService.exe and click the Attach button that should be enabled. Now you will enter the debug mode.
- 11/24/2011
- Zuoliu DING
- 11/28/2011
- zding
http://msdn.microsoft.com/en-us/library/yzk7ksy2.aspx
"Service Application Programming Architecture"
OnStart() can not be long running; it must start in 30 seconds (http://msdn.microsoft.com/en-us/library/kbe0xeh6.aspx). For that reason, OnStart() is not easy to jump into.
My method places kludgeMyWindowsServiceBreakpoint() in my service. I also place a 120 second delay at the beginning off my service:
System.Threading.Thread.Sleep(120 * 1000); // convert seconds to milliseconds and sleep
kludgeMyWindowsServiceBreakpoint();
public void kludgeMyWindowsServiceBreakpoint()
{
}
The kludgeMyWindowsServiceBreakpoint() gives vs2010 an anchor to grab.
Step 1: put the text "kludgeMyWindowsServiceBreakpoint" on the clipboard (without the quotes).
Step 2: run vs2010 as Administrator.
Step 3: in vs2010, Debug, Attach to Process...
Step 4: in the Attach to Process dialog, check
[x] Show processes for all users
and [x] Show processes in all sessions
Step 5: start Services
Step 6: start your service; you now have approximately two minutes for the following steps.
Step 7: Atl+Tab back to vs2010
Step 8: in vs2010, in the Attach to Process dialog, click Refresh
Step 9: locate and select your service in the Available Processes list
Step 10: click Attach
Step 11: in vs2010, Debug, New Breakpoint, Break at Function.... (or Ctrl+d,n)
Step 12: paste "kludgeMyWindowsServiceBreakpoint" in the Function text box, check
[x] Use IntelliSense to verify the function name
and select your coding language from the Language drop down.
Step 13: Click OK
Step 14: If IntelliSense can not locate "kludgeMyWindowsServiceBreakpoint",
do not worry. Just click OK and continue. The New Breakpoint dialog with close.
Step 15: Check that your attach was successful, in vs2010, Debug, Windows, Breakpoints (or Ctrl+d,b)
[if your breakpoint was successfully set, you'll see the fully qualified name
of "kludgeMyWindowsServiceBreakpoint" in the vs2010 Breakpoints window.]
Step 16: if you were successful, your service will break at "kludgeMyWindowsServiceBreakpoint".
Note, randomly the above process will fail. Usually removing your service, rebuilding your
service project, and reinstalling your service will be required. Sometimes you may need
to do this more than once. Debugging Windows services can be both physical and pyschological torture.
"chasing a random bug inside of a Windows service is like trying to reach the pot of gold at the end of a rainbow."
- 8/26/2011
- gerry lowry
1. Modify the OnStart method like this:
protected override void OnStart( string[] args ) { Thread.Sleep( 15000 );2. Install service ( e.g. InstallUtil.exe )
// Start code here
}
(don't put it higher than 30.000 )
3. NET STOP <ServicName> (if it automatically started )
4. In VS click Debug ->Attach to Process window
5. NET START <ServicName> ( The service process in visible now in Attach to Process window)
6. In VS Attach to Process window hit Refresh and select you service process ( do this in at most 15 seconds )
7. Happy Debugging
- 8/5/2011
- Neuromaster
Here is an article explaining what you must do. It's painless.
http://support.microsoft.com/kb/929664
-Brian Jasmer
- 11/15/2010
- bjasmer
Caution