Click here to Skip to main content
13,792,869 members
Click here to Skip to main content
Add your own
alternative version


58 bookmarked
Posted 7 Aug 2007
Licenced CPOL

How to debug a Windows Service and not die in the effort

, 14 Aug 2007
Rate this:
Please Sign up or sign in to vote.
A method to debug any part of a Windows Service.


Windows Services is a powerful programming tool that the Windows OS give us to run programs without the need for a human user logged in to the system.

Due the Windows Service nature, Visual Studio cannot directly launch a service and debug it. Also, you cannot attach a service to debug the service constructor or to debug the Start event. That is because you cannot attach a process that does not exist!

There exists different solutions: one of these is explained in an excellent article by Lee Humphries. But, what about attaching the debugger at the program point where we want to begin to debug? Well, it is possible in .NET 2.0, you only need to use the Debugger class from System.Diagnostic namespace.


Debugger class in System.Diagnostic has an important method for our purposes: Debugger.Launch().

When Debugger.Launch() is executed, it watches if the actual process is attached to any debugger; if it is not, it opens the debugger attachment windows and prompt us to select the appropriate debugger to use with the program. Then, you only need to locate the call to Launch() where you want to begin controlling the service.

See the following code an example to init a debugger in the OnStart event of a Windows Service:

protected override void OnStart(string[] args)
    iTest = 1; 
#if (DEBUG) 
    Debugger.Launch(); //<-- Simple form to debug a web services 
    timer1.Enabled = true; 

When the framework executes the Debugger.Launch method and prompts you to select the debugger, select the VS instance that shows the service code and continue to run the service under the control of the debugger.

You need to surround the instruction with a #if #endif clause because the method Debugger.Launch() also executes in Release mode (that should not happen, but .NET 2.0 has a bug in the Debugger.Launch() method).

Using the code

  1. Download and compile the code attached to the article with the DEBUG option.
  2. The code is a simple Windows Service that uses a timer to increment an integer value with an interval of 5 seconds. The service writes the value of the integer variable in the output windows. Take a look at the the Launch() method in the code.

  3. Install the Windows Service. I recommend automating this task. Personally, I created in the Tool -> External Tool menu, two inputs: one to install and another to uninstall the service. See the image below:
  4. Screenshot - HowtoDebugaWindowsService.png

    If you have another system to install/uninstall the service, skip the following description:

    To create an entry to install the example Windows Service, click Tool -> External Tool -> Add, and enter:

    • Title: InstallService
    • Command: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe (use the directory of your Windows installation)
    • Argument: WServiceDebug.exe
    • Initial Directory: $(ProjectDir)/bin/debug

    To create an entry to uninstall the example Windows Service, click Tool -> External Tool -> Add, and enter:

    • Title: UnInstallService
    • Command: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe (use the directory of your Windows installation)
    • Argument: /u WServiceDebug.exe
    • Initial Directory: $(ProjectDir)/bin/debug
  5. In Visual Studio, see that the code page of the Web Service is open.
  6. Start the service. Open the Services applet: Control Panel -> Administrative Tools - > Services. You will see that the service named WServiceDebug is not started. Right clickand select Start. You can see the dialog to select the debugger is open. Select the instance of Visual Studio that has the Windows Service project open. Go to Visual Studio, see that the program is stopped and ready to debug. Press F5 and see in the Output windows that the timer writes the value of the integer value in 5-second intervals. Do not start a new debugger; use the Visual Studio instance when you have the service compiled.
  7. Set a break point in the Stop procedure and stop the service. You will see that the program stops at the break point, Press F5 to end the debug session.
  8. Uninstall the Windows Service (use the shortcut created in point 3). Recompile the application but in Release mode. Then, re-install the Windows Service and start it (you must modify the shortcut to install/uninstall to the initial directory: $(ProjectDir)/bin/release. You will see that the invocation to the debugger will not succeed and the service starts normally.
  9. Recompile the program in Debug mode and experiment with different situations of the Launch() method inside the service code.

Points of interest

Using the System.Debugger.Launch() method is a simple solution to resolve the problem of debugging Windows Services. I suppose that a lot of you are using this technique, but I have not found much information about this (I have not done much research).


  • 07 August 2007 - First version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Avalon Development
United States United States
Jose A. Garcia Guirado, Electronic Engineer, graduated in Havana/Cuba 1982, MCTS, MCSD.NET, MCAD.NET, MCSE. Worked in the Institute for Cybernetics and Mathematics of Academy of Science of Cuba for 8 years; since 1995 working as free software architect, developer and adviser, first in Argentina and from 2003 to 2010, in Germany as External consultant in DWS Luxembourg, AIXTRON AG and Shell Deutschland GmbH and from 2010 to 2012 in Mexico working for Twenty Century Fox, and Mexico Stock Exchange (BMV). From 2010 to now in USA, Florida, First in FAME Inc. and now as Senior Software Engineer in Spirit Airlines.

You may also be interested in...

Comments and Discussions

GeneralTry this method Pin
Anderson Imes22-Aug-07 15:01
memberAnderson Imes22-Aug-07 15:01 
GeneralLife Saver Pin
thund3rstruck15-Aug-07 5:40
memberthund3rstruck15-Aug-07 5:40 
GeneralRe: Life Saver Pin
freedeveloper16-Aug-07 2:08
professionalfreedeveloper16-Aug-07 2:08 
GeneralFYI: .NET 1.1 also supports Debugger.Launch() Pin
Michael B. Hansen15-Aug-07 1:06
memberMichael B. Hansen15-Aug-07 1:06 
GeneralRe: FYI: .NET 1.1 also supports Debugger.Launch() Pin
freedeveloper16-Aug-07 1:11
professionalfreedeveloper16-Aug-07 1:11 
GeneralOther options [modified] Pin
Alek Davis14-Aug-07 8:09
memberAlek Davis14-Aug-07 8:09 
AnswerRe: Other options Pin
freedeveloper19-Aug-07 6:35
professionalfreedeveloper19-Aug-07 6:35 
GeneralHandy Pin
cliff hewett13-Aug-07 13:35
professionalcliff hewett13-Aug-07 13:35 
This is a handy tip, which I wasn't aware of. Thanks.

The Boogie Man

AnswerRe: Handy Pin
freedeveloper19-Aug-07 6:13
professionalfreedeveloper19-Aug-07 6:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.181207.3 | Last Updated 15 Aug 2007
Article Copyright 2007 by freedeveloper
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid