5,778,647 members and growing! (22,405 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: 124,703
Bookmarked: 104 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
53 votes for this Article.
Popularity: 7.90 Rating: 4.58 out of 5
3 votes, 5.7%
1
2 votes, 3.8%
2
1 vote, 1.9%
3
8 votes, 15.1%
4
39 votes, 73.6%
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 52 (Total in Forum: 52) (Refresh)FirstPrevNext
QuestionCan you use OnStop()???memberkeith shumway9:25 13 Nov '08  
AnswerRe: Can you use OnStop()???memberLee Humphries11:32 13 Nov '08  
GeneralRe: Can you use OnStop()???memberkeith shumway13:01 13 Nov '08  
GeneralExcellent Tip..memberMember 397871123:20 16 Oct '08  
GeneralNice!memberDanie de Kock3:12 1 Oct '08  
GeneralGood, but...memberAndreas Saurwein Franci Gonçalves8:53 30 Sep '08  
GeneralGood jobmemberThe Ruler23:57 18 Jul '08  
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  

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-2009
Web13 | Advertise on the Code Project