Click here to Skip to main content
Licence CPOL
First Posted 18 Apr 2005
Views 258,376
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
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  
QuestionA Cleaner Way? Pinmembersstreaker3:38 8 Jul '07  
AnswerRe: 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  
If you're simply cutting and pasting from the article - then you won't get anywhere. You need to follow the instructions in the article and create a new C# Windows Service project in Visual Studio, once you've done that you'll be left with a service class called Service1. You would normally change this name to something more suitable and put in your current Service code.
 
The Service1 code itself would probably look like:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
 
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
 
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
}
 
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
}
}

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  
GeneralRe: Timer event isn't called PinmemberLee Humphries18:09 10 Apr '07  
GeneralRe: Timer event isn't called PinmemberAnderson Imes4:07 18 Apr '07  
GeneralGreat piece of code Pinmemberjimcmt1:56 1 Mar '07  
GeneralGood stuff...and so easy Pinmembercraig.w15:50 7 Feb '07  
GeneralGood Job! Pinmemberrichan0:03 7 Jan '07  
JokeI stole your idea PinmemberAnderson Imes4:10 28 Dec '06  
GeneralRe: I stole your idea PinmemberLee Humphries12:09 28 Dec '06  
GeneralRe: I stole your idea PinmemberAnderson Imes13:57 28 Dec '06  
GeneralDEBUG switch PinmemberGnuconcepts4:38 17 Nov '06  
GeneralRe: DEBUG switch PinmemberLee Humphries15:29 19 Nov '06  
GeneralThank You Pinmemberzeineddine7:16 15 Sep '06  
GeneralVery nice! Pinmemberdave_ferreira7:18 22 Aug '06  
GeneralRe: Very nice! PinmemberLee Humphries15:55 22 Aug '06  
GeneralRe: Very nice! Pinmemberdave_ferreira16:01 22 Aug '06  

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
Web03 | 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