Click here to Skip to main content
Licence CPOL
First Posted 12 Sep 2007
Views 20,409
Bookmarked 15 times

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

By | 12 Sep 2007 | Article
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

Thomas Maierhofer

CEO
MSE-iT
Germany Germany

Member

I'm the CEO of MSE-iT Reisebürosoftware, a software development company in Germany making travel agency accounting software.
 
We release some library stuff under GPL or LGPL, so everybody can use it. These are our Open-Source projects so far:
 
Spell Checking, Hyphenation and Thesaurus for .NET

.NET spell checker, hyphenation and thesaurus based on the Open Offlice spell checker Hunspell. Here is a life Demo: Spell check, hyphenation and thesaurus reference project for NHunspell on ASP.NET.
 
jQuery Plugins

jQuery Background Canvas Plugin Inserts a HTML5 CANVAS element behind any HTML element and allows to draw on it with JavaScript.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSimpler solution PinmemberJulien Couvreur11:32 13 Sep '07  
AnswerRe: Simpler solution - but not nice PinmemberMSE-iT15:37 13 Sep '07  
GeneralDebugger.Launch(); PinmemberMladen Jankovic11:53 12 Sep '07  
GeneralRe: Debugger.Launch(); [modified] PinmemberMSE-iT22:22 12 Sep '07  
GeneralRe: Debugger.Launch(); PinmemberEduard Gomolyako1:30 13 Sep '07  
AnswerRe: Debugger.Launch(); PinmemberMSE-iT2:17 13 Sep '07  
AnswerRe: Debugger.Launch(); PinmemberMladen Jankovic5:57 13 Sep '07  
AnswerRe: Debugger.Launch(); PinmemberMSE-iT6:11 13 Sep '07  
GeneralSolution for Debugger.Launch PinmemberKyronSr8:13 8 Jul '08  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 12 Sep 2007
Article Copyright 2007 by Thomas Maierhofer
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid