Skip to main content
Email Password   helpLost your password?

Introduction

Normally, debugging a Windows service under Visual Studio .NET is painful. Windows services won't actually run directly within Visual Studio .NET, so the usual technique is to install and start the Windows service and then attach a debugger to it. An alternative approach is to pull the guts out of the service, stick it in a separate library, and then build some other app (e.g., a console app) to sit in front of it. This approach uses neither of those techniques.

When building a C# Windows Service project in Visual Studio, it will leave you with a class containing quite a few methods including a Main(), such as this:

// The main entry point for the process

static void Main()
{
    System.ServiceProcess.ServiceBase[] ServicesToRun;

    // More than one user Service may run within the same process. To add

    // another service to this process, change the following line to

    // create a second service object. For example,

    //

    // ServicesToRun = new 

    //      System.ServiceProcess.ServiceBase[] {new Service1(), 

    //      new MySecondUserService()};

    //


    ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
    System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

Obviously, it's the Main() above that ends up executing the service, and it's the Main() that this approach manipulates so that the Windows Service can be debugged directly within Visual Studio .NET.

Using the example above (and removing some of the comments), here's how:

// The main entry point for the process

static void Main()
{
#if (!DEBUG)
    System.ServiceProcess.ServiceBase[] ServicesToRun;
    ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
    System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#else
    // Debug code: this allows the process to run as a non-service.

    // It will kick off the service start point, but never kill it.

    // Shut down the debugger to exit

    Service1 service = new Service1();
    service.<Your Service's Primary Method Here>();
    // Put a breakpoint on the following line to always catch
    // your service when it has finished its work
    System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#endif 
}

It's crude, but effective (CBE - also known as Commander of the British Empire ;)). Run the service in debug mode to debug it, compile and install it as a release build, and it's a full and proper Windows service.

You may still wish to pull the guts out of your service into a separate library for unit testing. But this approach allows you to work with almost all of your service code as an actual service.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralI'm still not understanding something... Pin
Ray Mitchell
13:57 23 Sep '09  
Generalanother way to create, debug, instal windows service.... Pin
GamePlanner
15:36 7 Apr '09  
GeneralAwesome! Pin
Jammer
5:41 27 Mar '09  
Generaltime saving and powerful! Pin
thorleifs
8:56 20 Mar '09  
GeneralExtremely helpful .. Caught my admiration Pin
hackrogenius
11:23 3 Mar '09  
GeneralNiceness! Pin
progrez
23:58 8 Jan '09  
QuestionCan you use OnStop()??? Pin
keith shumway
9:25 13 Nov '08  
AnswerRe: Can you use OnStop()??? Pin
Lee Humphries
11:32 13 Nov '08  
GeneralRe: Can you use OnStop()??? Pin
keith shumway
13:01 13 Nov '08  
GeneralExcellent Tip.. Pin
Member 3978711
23:20 16 Oct '08  
GeneralNice! Pin
Danie de Kock
3:12 1 Oct '08  
GeneralGood, but... Pin
Andreas Saurwein Franci Gonçalves
8:53 30 Sep '08  
GeneralGood job Pin
The Ruler
23:57 18 Jul '08  
GeneralABS love it !!! Pin
izmoto
2:44 2 Apr '08  
GeneralAnother way to do it Pin
Einar Egilsson
8:14 15 Aug '07  
GeneralRe: Another way to do it Pin
nnm
13:34 28 Aug '07  
GeneralNICE, EXCELLENT!!!! Pin
Balder1978-2
8:54 14 Aug '07  
GeneralA Cleaner Way? Pin
sstreaker
3:38 8 Jul '07  
GeneralRe: A Cleaner Way? Pin
Lee Humphries
14:37 8 Jul '07  
GeneralI dont get it [modified] Pin
TEMoore
12:57 29 May '07  
GeneralRe: I dont get it Pin
Lee Humphries
13:27 29 May '07  
GeneralRe: I dont get it Pin
TEMoore
6:21 30 May '07  
GeneralRe: I dont get it Pin
Lee Humphries
17:12 30 May '07  
GeneralElegant Solution Pin
John-Howard
9:16 15 May '07  
GeneralTimer event isn't called Pin
Nicolas Stuardo
12:13 10 Apr '07  


Last Updated 14 Aug 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009