Click here to Skip to main content
Licence CPOL
First Posted 18 Apr 2005
Views 258,439
Bookmarked 145 times

Debugging Windows Services under Visual Studio .NET

By Lee Humphries | 14 Aug 2006
How to 'fudge' Windows Services code so that it can be debugged under Visual Studio .NET.
3 votes, 3.5%
1
2 votes, 2.4%
2
1 vote, 1.2%
3
8 votes, 9.4%
4
71 votes, 83.5%
5
4.90/5 - 85 votes
6 removed
μ 4.69, σa 1.59 [?]

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

Architect

Australia Australia

Member
If it ain't broke - that can be arranged.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionLife saver! PinadminChris Maunder12:54 12 Oct '11  
QuestionTry Windows Service Helper Pinmemberbreakpoint17:44 20 Sep '11  
GeneralSimple things are often the best! PinmemberMember 385355322:48 21 Jul '11  
GeneralMy vote of 5 PinmemberDragonLord660:24 4 May '11  
GeneralMy vote of 5 Pinmembercable beach18:30 20 Apr '11  
GeneralDebugging Windows service in simple way PinmemberKrishna Mohan Reddy N4:53 8 Apr '11  
GeneralRe: Debugging Windows service in simple way PinmemberLee Humphries14:50 10 Apr '11  
GeneralMy vote of 5 Pinmemberrohancragg0:11 7 Jan '11  
GeneralMy vote of 5 Pinmembervisionmaster50510:12 29 Dec '10  
GeneralMy vote of 5 Pinmemberalexdresko10:40 17 Nov '10  
GeneralMy vote of 5 PinmemberMuraliDhar_A1:45 20 Aug '10  
Generalsuper thx !! Pinmemberfasafr2:34 26 Apr '10  
GeneralIt worked. Thanks! PinmemberVijay Paramasivam Rajasekaran6:56 12 Apr '10  
GeneralI'm still not understanding something... PinmemberRay Mitchell13:57 23 Sep '09  
GeneralRe: I'm still not understanding something... PinmemberLee Humphries11:42 17 Apr '10  
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  
It depends on what your bug is.
 
If it's just a problem within the OnStop then change the call to sleep forever below with a call to your OnStop.
 
// 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);
 
However if it's the actual calling of OnStop that seems to be the problem, then I'd suggest reading through some of the earlier comments as there are more than a few people who've taken this idea and gone even further with it.
 
I just love Koalas - they go great with Bacon.

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  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120210.1 | Last Updated 14 Aug 2006
Article Copyright 2005 by Lee Humphries
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid