Click here to Skip to main content
Click here to Skip to main content

Debugging OnStart Method in .NET Service when Debbuger.Launch Doesn't Work

, 12 Sep 2007
Rate this:
Please Sign up or sign in to vote.
Technique to attach a debugger to a starting service project

Introduction

A lot of workarounds exist to debug the OnStart method of a service. A straight way with Debugger.Launch() is described in How to Debug a Windows Service. But what if this wouldn't work, e.g. on Windows Vista x64? This sample shows a simple method to attach the debugger to the service process and debug the OnStart method.

How It Works

The service process should wait till you have attached the debugger. The service process should not timeout. Just look at the code, it's quite easy.

Using the Code

All you have to do, is to copy this code snippet in your Onstart method of your service class (derived from ServiceBase).

Using Directives

using System.Diagnostics;  // a quite useful namespace for debugging issues
using System.Threading;

OnStart Method

protected override void OnStart(string[] args)
{
#if DEBUG
    while (!Debugger.IsAttached)      // Waiting until debugger is attached
    {
        RequestAdditionalTime(1000);  // Prevents the service from timeout
        Thread.Sleep(1000);           // Gives you time to attach the debugger   
    }
    RequestAdditionalTime(20000);     // for Debugging the OnStart method,
                                      // increase as needed to prevent timeouts
#endif
    
    // here is your startup code with breakpoints
} 

Start the service as usual. The service will wait until you attach the debugger to the service process. You can set breakpoints in your start code and debug it.

History

  • 12th September, 2007: Initial version tested on Vista x64 / VS2005 with a x64 compiled C# project

License

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

About the Author

I'm working on a new project called Crawler-Lib. It is a generalized back-end processing and hosting framework. Please take a look at it: Crawler-Lib Homepage
 
I have also a YouTube channel for this: Crawler-Lib YouTube Channel

Comments and Discussions

 
GeneralSimpler solution PinmemberJulien Couvreur13-Sep-07 11:32 
AnswerRe: Simpler solution - but not nice PinmemberMSE-iT13-Sep-07 15:37 
GeneralDebugger.Launch(); PinmemberMladen Jankovic12-Sep-07 11:53 
Debugger.Launch() can do the job.
 
Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

GeneralRe: Debugger.Launch(); [modified] PinmemberMSE-iT12-Sep-07 22:22 
GeneralRe: Debugger.Launch(); PinmemberEduard Gomolyako13-Sep-07 1:30 
AnswerRe: Debugger.Launch(); PinmemberMSE-iT13-Sep-07 2:17 
AnswerRe: Debugger.Launch(); PinmemberMladen Jankovic13-Sep-07 5:57 
AnswerRe: Debugger.Launch(); PinmemberMSE-iT13-Sep-07 6:11 
GeneralSolution for Debugger.Launch PinmemberKyronSr8-Jul-08 8:13 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 12 Sep 2007
Article Copyright 2007 by Thomas Maierhofer (Tom)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid