Click here to Skip to main content
15,868,164 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
3.86/5 (21 votes)
14 Aug 2007CPOL4 min read 130.5K   823   57   29
A method to debug any part of a Windows Service.

Introduction

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.

Background

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:

C#
protected override void OnStart(string[] args)
{
    iTest = 1; 
#if (DEBUG) 
    Debugger.Launch(); //<-- Simple form to debug a web services 
#endif 
    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).

History

  • 07 August 2007 - First version.

License

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


Written By
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 2013 to now in USA, Florida, First in FAME Inc. and now as Senior Software Engineer in Spirit Airlines.

Comments and Discussions

 
SuggestionSolution that doesn't require source access Pin
Nathan J Kidd4-Apr-19 4:54
Nathan J Kidd4-Apr-19 4:54 
GeneralMy vote of 5 Pin
menarenprvn22-Oct-12 4:14
menarenprvn22-Oct-12 4:14 
GeneralRe: My vote of 5 Pin
freedeveloper22-Oct-12 6:25
professionalfreedeveloper22-Oct-12 6:25 
GeneralMy vote of 5 Pin
VickyC#19-Feb-11 17:56
VickyC#19-Feb-11 17:56 
GeneralRe: My vote of 5 Pin
freedeveloper27-Oct-11 7:07
professionalfreedeveloper27-Oct-11 7:07 
GeneralUse solution from "Debugging Windows Services is a Pain" instead Pin
Michael Freidgeim8-Dec-08 22:55
Michael Freidgeim8-Dec-08 22:55 
GeneralRe: Use solution from "Debugging Windows Services is a Pain" instead Pin
jgauffin23-Aug-10 1:33
jgauffin23-Aug-10 1:33 
It takes traffic because it's popular not because google thinks it's a fun article. Unsure | :~
GeneralMy vote of 1 Pin
Michael Freidgeim8-Dec-08 22:47
Michael Freidgeim8-Dec-08 22:47 
GeneralGood article Pin
Klaasjan Mors10-Nov-08 20:34
Klaasjan Mors10-Nov-08 20:34 
AnswerRe: Good article Pin
freedeveloper11-Nov-08 10:27
professionalfreedeveloper11-Nov-08 10:27 
GeneralVery good article Pin
Viresh Shah18-Jul-08 23:21
Viresh Shah18-Jul-08 23:21 
GeneralVista Business Pin
kalayni9-Jun-08 22:01
kalayni9-Jun-08 22:01 
GeneralVery Helpful article Pin
Awais Usman Butt27-Mar-08 21:18
Awais Usman Butt27-Mar-08 21:18 
GeneralMy way Pin
PIEBALDconsult27-Nov-07 12:20
mvePIEBALDconsult27-Nov-07 12:20 
GeneralGood stuff Jose. Pin
Krish Van Colombo27-Nov-07 11:31
Krish Van Colombo27-Nov-07 11:31 
GeneralSuggested Improvement - External Tools Pin
Mr 458246614-Nov-07 13:35
Mr 458246614-Nov-07 13:35 
GeneralExcellent Pin
sunilvelhal6-Nov-07 12:43
sunilvelhal6-Nov-07 12:43 
GeneralRe: Excellent Pin
freedeveloper10-Nov-07 2:52
professionalfreedeveloper10-Nov-07 2:52 
GeneralDebugger.Launch() wont' work on Vista x64 Pin
Thomas Maierhofer (Tom)12-Sep-07 23:15
Thomas Maierhofer (Tom)12-Sep-07 23:15 
GeneralRe: Debugger.Launch() wont' work on Vista x64 Pin
freedeveloper13-Sep-07 1:09
professionalfreedeveloper13-Sep-07 1:09 
GeneralTry this method Pin
Anderson Imes22-Aug-07 14:01
Anderson Imes22-Aug-07 14:01 
GeneralLife Saver Pin
thund3rstruck15-Aug-07 4:40
thund3rstruck15-Aug-07 4:40 
GeneralRe: Life Saver Pin
freedeveloper16-Aug-07 1:08
professionalfreedeveloper16-Aug-07 1:08 
GeneralFYI: .NET 1.1 also supports Debugger.Launch() Pin
Michael B. Hansen15-Aug-07 0:06
Michael B. Hansen15-Aug-07 0:06 
GeneralRe: FYI: .NET 1.1 also supports Debugger.Launch() Pin
freedeveloper16-Aug-07 0:11
professionalfreedeveloper16-Aug-07 0:11 

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.