Click here to Skip to main content
13,901,646 members
Click here to Skip to main content
Add your own
alternative version

Stats

534.5K 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

 
GeneralMy vote of 5 Pin
visionmaster50529-Dec-10 9:12
membervisionmaster50529-Dec-10 9:12 
GeneralMy vote of 5 Pin
alexdresko17-Nov-10 9:40
memberalexdresko17-Nov-10 9:40 
GeneralMy vote of 5 Pin
MuraliDhar_A20-Aug-10 0:45
memberMuraliDhar_A20-Aug-10 0:45 
Generalsuper thx !! Pin
fasafr26-Apr-10 1:34
memberfasafr26-Apr-10 1:34 
GeneralIt worked. Thanks! Pin
Vijay Paramasivam Rajasekaran12-Apr-10 5:56
memberVijay Paramasivam Rajasekaran12-Apr-10 5:56 
GeneralI'm still not understanding something... Pin
Ray Mitchell23-Sep-09 12:57
memberRay Mitchell23-Sep-09 12:57 
GeneralRe: I'm still not understanding something... Pin
Lee Humphries17-Apr-10 10:42
professionalLee Humphries17-Apr-10 10:42 
Generalanother way to create, debug, instal windows service.... Pin
GamePlanner7-Apr-09 14:36
memberGamePlanner7-Apr-09 14:36 
GeneralAwesome! Pin
Jammer27-Mar-09 4:41
memberJammer27-Mar-09 4:41 
Generaltime saving and powerful! Pin
thorleifs20-Mar-09 7:56
memberthorleifs20-Mar-09 7:56 
GeneralExtremely helpful .. Caught my admiration Pin
Asif Ashraf3-Mar-09 10:23
professionalAsif Ashraf3-Mar-09 10:23 
GeneralNiceness! Pin
itfoqus8-Jan-09 22:58
memberitfoqus8-Jan-09 22:58 
QuestionCan you use OnStop()??? Pin
keith shumway13-Nov-08 8:25
memberkeith shumway13-Nov-08 8:25 
AnswerRe: Can you use OnStop()??? Pin
Lee Humphries13-Nov-08 10:32
professionalLee Humphries13-Nov-08 10:32 
GeneralRe: Can you use OnStop()??? Pin
keith shumway13-Nov-08 12:01
memberkeith shumway13-Nov-08 12:01 
GeneralExcellent Tip.. Pin
Member 397871116-Oct-08 22:20
memberMember 397871116-Oct-08 22:20 
GeneralNice! Pin
Danie de Kock1-Oct-08 2:12
memberDanie de Kock1-Oct-08 2:12 
GeneralGood, but... Pin
Andreas Saurwein Franci Gonçalves30-Sep-08 7:53
memberAndreas Saurwein Franci Gonçalves30-Sep-08 7:53 
GeneralGood job Pin
Viresh Shah18-Jul-08 22:57
memberViresh Shah18-Jul-08 22:57 
GeneralABS love it !!! Pin
izmoto2-Apr-08 1:44
memberizmoto2-Apr-08 1:44 
GeneralAnother way to do it Pin
Einar Egilsson15-Aug-07 7:14
memberEinar Egilsson15-Aug-07 7:14 
GeneralRe: Another way to do it Pin
nnm28-Aug-07 12:34
membernnm28-Aug-07 12:34 
GeneralNICE, EXCELLENT!!!! Pin
hcihlen14-Aug-07 7:54
memberhcihlen14-Aug-07 7:54 
QuestionA Cleaner Way? Pin
sstreaker8-Jul-07 2:38
membersstreaker8-Jul-07 2:38 
AnswerRe: A Cleaner Way? Pin
Lee Humphries8-Jul-07 13:37
professionalLee Humphries8-Jul-07 13:37 

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