5,316,172 members and growing! (20,092 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » How To     Intermediate License: The Code Project Open License (CPOL)

Debugging Windows Services under Visual Studio .NET

By Lee Humphries

How to 'fudge' Windows Services code so that it can be debugged under Visual Studio .NET.
C#, Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 18 Apr 2005
Updated: 14 Aug 2006
Views: 106,422
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
49 votes for this Article.
Popularity: 7.69 Rating: 4.55 out of 5
3 votes, 6.1%
1
2 votes, 4.1%
2
1 vote, 2.0%
3
8 votes, 16.3%
4
35 votes, 71.4%
5

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)

About the Author

Lee Humphries


20 years in IT.
Motto: poke it with a stick and see if it twitches.
Occupation: Other
Location: Solomon Islands Solomon Islands

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 46 (Total in Forum: 46) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralGood jobmemberThe Ruler5hrs 45mins ago 
GeneralABS love it !!!memberizmoto2:44 2 Apr '08  
GeneralAnother way to do itmemberEinar Egilsson8:14 15 Aug '07  
GeneralRe: Another way to do itmembernnm13:34 28 Aug '07  
GeneralNICE, EXCELLENT!!!!memberBalder1978-28:54 14 Aug '07  
GeneralA Cleaner Way?membersstreaker3:38 8 Jul '07  
GeneralRe: A Cleaner Way?memberLee Humphries14:37 8 Jul '07  
GeneralI dont get it [modified]memberTEMoore12:57 29 May '07  
GeneralRe: I dont get itmemberLee Humphries13:27 29 May '07  
GeneralRe: I dont get itmemberTEMoore6:21 30 May '07  
GeneralRe: I dont get itmemberLee Humphries17:12 30 May '07  
GeneralElegant SolutionmemberJohn-Howard9:16 15 May '07  
GeneralTimer event isn't calledmemberNicolas Stuardo12:13 10 Apr '07  
GeneralRe: Timer event isn't calledmemberLee Humphries18:09 10 Apr '07  
GeneralRe: Timer event isn't calledmemberAnderson Imes4:07 18 Apr '07  
GeneralGreat piece of codememberjimcmt1:56 1 Mar '07  
GeneralGood stuff...and so easymembercraig.w15:50 7 Feb '07  
GeneralGood Job!memberrichan0:03 7 Jan '07  
JokeI stole your ideamemberAnderson Imes4:10 28 Dec '06  
GeneralRe: I stole your ideamemberLee Humphries12:09 28 Dec '06  
GeneralRe: I stole your ideamemberAnderson Imes13:57 28 Dec '06  
GeneralDEBUG switchmemberGnuconcepts4:38 17 Nov '06  
GeneralRe: DEBUG switchmemberLee Humphries15:29 19 Nov '06  
GeneralThank Youmemberzeineddine7:16 15 Sep '06  
GeneralVery nice!memberdave_ferreira7:18 22 Aug '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Aug 2006
Editor: Smitha Vijayan
Copyright 2005 by Lee Humphries
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project