4 out of 6 rated this helpful - Rate this topic

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 noteCaution

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.

NoteNote

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

  1. Install your service. For more information, see How to: Install and Uninstall Services.

  2. Start your service, either from Services Control Manager, Server Explorer, or from code. For more information, see How to: Start Services.

  3. In Visual Studio, choose Attach to Process from the Debug menu.

    The Processes dialog box appears.

  4. Click Show system processes.

  5. In the Available Processes section, click the process for your service, and then click Attach.

    TipTip

    The process will have the same name as the executable file for your service.

    The Attach to Process dialog box appears.

  6. Choose any appropriate options, and then click OK to close the dialog box.

    NoteNote

    You are now in debug mode.

  7. Set any breakpoints you want to use in your code.

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

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Debugging OnStart() method
What I did was enable my service to handle pause and continue events. I call OnStart() in the OnContinue() method.  Maybe this will help someone else.
No Processes menu
In Visual Studio 2010 there is no Processes menu item in the Debug menu, there is only the Attach to Process menu item (which was added by importing the C# development settings). In the Attach to Processes form there is no Show system processes checkbox. So how do I debug windows services?
How to solve: Precess Greyed Out / Disabled Edit
To attach your service to the VS 2010 debugger, the following are a must:
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.
Precess is Greyed Out / Disabled
I have followed the instructions here but in the Attach to Process dialog my process is listed, but it's in grey, and when I select it the "Attach" button becomes disabled. $0$0 $0 $0Additional Info:$0 $0In the Type column it says Managed (v4.0.30319), ProteusDebugEngine.$0 $0In the Attach to: text box it says "Automatic: Trace code, Managed code" by default.$0 $0$0 $0 $0Any ideas? $0 $0Thanks, Jo$0$0 $0$0 $0 $0More than likely you are looking at the Visual Studio instance, as it will be listed in the processes by that name.  Click show all processes from all users, as the service won't be running as you.  You should then see two processes by that name, one will be the Visual Studio, the other will be the windows service.$0
A debugging method for Windows Service Applications
Architecturally, a Windows Service Application has a Main() that fires the OnStart().
      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."
Debugging the OnStart method

1. Modify the OnStart method like this:

  protected override void OnStart( string[] args )
{
			Thread.Sleep( 15000 );
// Start code here
}
(don't put it higher than 30.000 )
2. Install service ( e.g. InstallUtil.exe )
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

 
Processes not appearing on Debug Menu
If you attempt to debug a service but the Processes option isn't available on your Debug menu, you'll have to import some development environment settings.   For example, I had to import the C# development settings.

Here is an article explaining what you must do.  It's painless.

http://support.microsoft.com/kb/929664

-Brian Jasmer