Click here to Skip to main content
Click here to Skip to main content
Go to top

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.


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() };

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() };
    // 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

It's crude, but effective (CBE - also known as Commander of the British Empire Wink | ;) ). 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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Lee Humphries
Founder md8n
Australia Australia
If it ain't broke - that can be arranged.
Follow on   Twitter

Comments and Discussions

GeneralMy vote of 5 PinmemberDavid Rodriguez29-Apr-12 9:45 
GeneralMy vote of 5 Pinmembereka80824-Apr-12 4:50 
QuestionSo Cool! PinmemberMarlene Deyo20-Mar-12 4:18 
QuestionLife saver! PinadminChris Maunder12-Oct-11 11:54 
AnswerRe: Life saver! Pinmemberkicker20129-Jul-12 2:57 
QuestionTry Windows Service Helper Pinmemberbreakpoint20-Sep-11 16:44 
GeneralSimple things are often the best! PinmemberMember 385355321-Jul-11 21:48 
GeneralMy vote of 5 PinmemberDragonLord663-May-11 23:24 
GeneralMy vote of 5 Pinmembercable beach20-Apr-11 17:30 
GeneralDebugging Windows service in simple way PinmemberKrishna Mohan Reddy N8-Apr-11 3:53 
GeneralRe: Debugging Windows service in simple way PinmemberLee Humphries10-Apr-11 13:50 
GeneralMy vote of 5 Pinmemberrohancragg6-Jan-11 23:11 
GeneralMy vote of 5 Pinmembervisionmaster50529-Dec-10 9:12 
GeneralMy vote of 5 Pinmemberalexdresko17-Nov-10 9:40 
GeneralMy vote of 5 PinmemberMuraliDhar_A20-Aug-10 0:45 
Generalsuper thx !! Pinmemberfasafr26-Apr-10 1:34 
GeneralIt worked. Thanks! PinmemberVijay Paramasivam Rajasekaran12-Apr-10 5:56 
GeneralI'm still not understanding something... PinmemberRay Mitchell23-Sep-09 12:57 
GeneralRe: I'm still not understanding something... PinmemberLee Humphries17-Apr-10 10:42 
Generalanother way to create, debug, instal windows service.... PinmemberGamePlanner7-Apr-09 14:36 
GeneralAwesome! PinmemberJammer27-Mar-09 4:41 
Generaltime saving and powerful! Pinmemberthorleifs20-Mar-09 7:56 
GeneralExtremely helpful .. Caught my admiration Pinmemberhackrogenius3-Mar-09 10:23 
GeneralNiceness! Pinmemberprogrez8-Jan-09 22:58 
QuestionCan you use OnStop()??? Pinmemberkeith shumway13-Nov-08 8:25 
AnswerRe: Can you use OnStop()??? PinmemberLee Humphries13-Nov-08 10:32 
GeneralRe: Can you use OnStop()??? Pinmemberkeith shumway13-Nov-08 12:01 
GeneralExcellent Tip.. PinmemberMember 397871116-Oct-08 22:20 
GeneralNice! PinmemberDanie de Kock1-Oct-08 2:12 
GeneralGood, but... PinmemberAndreas Saurwein Franci Gonçalves30-Sep-08 7:53 
GeneralGood job PinmemberThe Ruler18-Jul-08 22:57 
GeneralABS love it !!! Pinmemberizmoto2-Apr-08 1:44 
GeneralAnother way to do it PinmemberEinar Egilsson15-Aug-07 7:14 
GeneralRe: Another way to do it Pinmembernnm28-Aug-07 12:34 
GeneralNICE, EXCELLENT!!!! PinmemberBalder1978-214-Aug-07 7:54 
QuestionA Cleaner Way? Pinmembersstreaker8-Jul-07 2:38 
AnswerRe: A Cleaner Way? PinmemberLee Humphries8-Jul-07 13:37 
GeneralI dont get it [modified] PinmemberTEMoore29-May-07 11:57 
GeneralRe: I dont get it PinmemberLee Humphries29-May-07 12:27 
GeneralRe: I dont get it PinmemberTEMoore30-May-07 5:21 
GeneralRe: I dont get it PinmemberLee Humphries30-May-07 16:12 
GeneralElegant Solution PinmemberJohn-Howard15-May-07 8:16 
GeneralTimer event isn't called PinmemberNicolas Stuardo10-Apr-07 11:13 
GeneralRe: Timer event isn't called PinmemberLee Humphries10-Apr-07 17:09 
GeneralRe: Timer event isn't called PinmemberAnderson Imes18-Apr-07 3:07 
GeneralGreat piece of code Pinmemberjimcmt1-Mar-07 0:56 
GeneralGood stuff...and so easy Pinmembercraig.w7-Feb-07 14:50 
GeneralGood Job! Pinmemberrichan6-Jan-07 23:03 
JokeI stole your idea PinmemberAnderson Imes28-Dec-06 3:10 
GeneralRe: I stole your idea PinmemberLee Humphries28-Dec-06 11:09 

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.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 14 Aug 2006
Article Copyright 2005 by Lee Humphries
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid