Click here to Skip to main content
14,024,164 members
Click here to Skip to main content
Add your own
alternative version

Stats

535.9K views
181 bookmarked
Posted 18 Apr 2005
Licenced CPOL

Debugging Windows Services under Visual Studio .NET

, 14 Aug 2006
Rate this:
Please Sign up or sign in to vote.
How to 'fudge' Windows Services code so that it can be debugged under Visual Studio .NET.

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.

License

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

Share

About the Author

Lee Humphries
Founder md8n
Timor-Leste Timor-Leste
If it ain't broke - that can be arranged.

You may also be interested in...

Pro

Comments and Discussions

 
GeneralTimer event isn't called Pin
Nicolas Stuardo10-Apr-07 11:13
memberNicolas Stuardo10-Apr-07 11:13 
GeneralRe: Timer event isn't called Pin
Lee Humphries10-Apr-07 17:09
professionalLee Humphries10-Apr-07 17:09 
GeneralRe: Timer event isn't called Pin
Anderson Imes18-Apr-07 3:07
memberAnderson Imes18-Apr-07 3:07 
GeneralGreat piece of code Pin
jimcmt1-Mar-07 0:56
memberjimcmt1-Mar-07 0:56 
GeneralGood stuff...and so easy Pin
craig.w7-Feb-07 14:50
membercraig.w7-Feb-07 14:50 
GeneralGood Job! Pin
richan6-Jan-07 23:03
memberrichan6-Jan-07 23:03 
JokeI stole your idea Pin
Anderson Imes28-Dec-06 3:10
memberAnderson Imes28-Dec-06 3:10 
GeneralRe: I stole your idea Pin
Lee Humphries28-Dec-06 11:09
professionalLee Humphries28-Dec-06 11:09 
Your solution looks like you actually thought about it.

I originally also borrowed the first idea of using a #DEBUG directive for debugging a service. Unfortunately the rest of that code was a bit too much 'crude' rather than 'effective'. But it has been interesting to watch the pieces come together on this issue. Actually that's kind of worrying considering how long ago I first wrote this article. Maybe Macroslop will *borrow* your idea as part of the default template for windows service projects.
GeneralRe: I stole your idea Pin
Anderson Imes28-Dec-06 12:57
memberAnderson Imes28-Dec-06 12:57 
GeneralDEBUG switch Pin
Gnuconcepts17-Nov-06 3:38
memberGnuconcepts17-Nov-06 3:38 
GeneralRe: DEBUG switch Pin
Lee Humphries19-Nov-06 14:29
professionalLee Humphries19-Nov-06 14:29 
GeneralThank You Pin
zeineddine15-Sep-06 6:16
memberzeineddine15-Sep-06 6:16 
GeneralVery nice! Pin
dferreira04222-Aug-06 6:18
memberdferreira04222-Aug-06 6:18 
GeneralRe: Very nice! Pin
Lee Humphries22-Aug-06 14:55
professionalLee Humphries22-Aug-06 14:55 
GeneralRe: Very nice! Pin
dferreira04222-Aug-06 15:01
memberdferreira04222-Aug-06 15:01 
JokeRe: Very nice! Pin
Lee Humphries22-Aug-06 16:20
professionalLee Humphries22-Aug-06 16:20 
GeneralRe: Very nice! Pin
dferreira04222-Aug-06 16:40
memberdferreira04222-Aug-06 16:40 
JokeRe: Very nice! Pin
Lee Humphries22-Aug-06 16:49
professionalLee Humphries22-Aug-06 16:49 
GeneralClever workaround! Pin
moriority511-Aug-06 11:05
membermoriority511-Aug-06 11:05 
GeneralRe: Clever workaround! Pin
Lee Humphries13-Aug-06 11:21
professionalLee Humphries13-Aug-06 11:21 
GeneralSolution to that Pin
Leo Davidson15-Aug-06 2:18
memberLeo Davidson15-Aug-06 2:18 
GeneralThanks - And Some Extra Stuff Pin
jriesen27-Jun-06 20:31
memberjriesen27-Jun-06 20:31 
GeneralRe: Thanks - And Some Extra Stuff Pin
Lee Humphries28-Jun-06 12:26
professionalLee Humphries28-Jun-06 12:26 
GeneralBest article !!! Pin
Phan Dung7-May-06 22:59
memberPhan Dung7-May-06 22:59 
GeneralWorks great Pin
pchelp25-Apr-06 10:34
memberpchelp25-Apr-06 10:34 

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.190419.4 | Last Updated 14 Aug 2006
Article Copyright 2005 by Lee Humphries
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid