Click here to Skip to main content
6,821,293 members and growing! (18,152 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, ASP.NET, Visual-Studio, Dev
Posted:18 Apr 2005
Updated:14 Aug 2006
Views:171,090
Bookmarked:118 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
61 votes for this article.
Popularity: 8.24 Rating: 4.62 out of 5
3 votes, 4.9%
1
2 votes, 3.3%
2
1 vote, 1.6%
3
8 votes, 13.1%
4
47 votes, 77.0%
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


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

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 58 (Total in Forum: 58) (Refresh)FirstPrevNext
GeneralI'm still not understanding something... PinmemberRay Mitchell13:57 23 Sep '09  
Generalanother way to create, debug, instal windows service.... PinmemberGamePlanner15:36 7 Apr '09  
GeneralAwesome! PinmemberJammer5:41 27 Mar '09  
Generaltime saving and powerful! Pinmemberthorleifs8:56 20 Mar '09  
GeneralExtremely helpful .. Caught my admiration Pinmemberhackrogenius11:23 3 Mar '09  
GeneralNiceness! Pinmemberprogrez23:58 8 Jan '09  
QuestionCan you use OnStop()??? Pinmemberkeith shumway9:25 13 Nov '08  
AnswerRe: Can you use OnStop()??? PinmemberLee Humphries11:32 13 Nov '08  
GeneralRe: Can you use OnStop()??? Pinmemberkeith shumway13:01 13 Nov '08  
GeneralExcellent Tip.. PinmemberMember 397871123:20 16 Oct '08  
GeneralNice! PinmemberDanie de Kock3:12 1 Oct '08  
GeneralGood, but... PinmemberAndreas Saurwein Franci Gonçalves8:53 30 Sep '08  
GeneralGood job PinmemberThe Ruler23:57 18 Jul '08  
GeneralABS love it !!! Pinmemberizmoto2:44 2 Apr '08  
GeneralAnother way to do it PinmemberEinar Egilsson8:14 15 Aug '07  
GeneralRe: Another way to do it Pinmembernnm13:34 28 Aug '07  
GeneralNICE, EXCELLENT!!!! PinmemberBalder1978-28:54 14 Aug '07  
GeneralA Cleaner Way? Pinmembersstreaker3:38 8 Jul '07  
GeneralRe: A Cleaner Way? PinmemberLee Humphries14:37 8 Jul '07  
GeneralI dont get it [modified] PinmemberTEMoore12:57 29 May '07  
GeneralRe: I dont get it PinmemberLee Humphries13:27 29 May '07  
GeneralRe: I dont get it PinmemberTEMoore6:21 30 May '07  
GeneralRe: I dont get it PinmemberLee Humphries17:12 30 May '07  
GeneralElegant Solution PinmemberJohn-Howard9:16 15 May '07  
GeneralTimer event isn't called PinmemberNicolas Stuardo12:13 10 Apr '07  

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

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