Click here to Skip to main content
15,889,867 members
Articles / Web Development / ASP.NET
Article

Debugging Windows Services under Visual Studio .NET

Rate me:
Please Sign up or sign in to vote.
4.91/5 (116 votes)
14 Aug 2006CPOL1 min read 603.4K   181   89
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:

C#
// 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:

C#
// 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)


Written By
Founder md8n
Timor-Leste Timor-Leste
If it ain't broke - that can be arranged.

Comments and Discussions

 
GeneralNice! Pin
Danie de Kock1-Oct-08 2:12
Danie de Kock1-Oct-08 2:12 
GeneralGood, but... Pin
Andreas Saurwein30-Sep-08 7:53
Andreas Saurwein30-Sep-08 7:53 
GeneralGood job Pin
Viresh Shah18-Jul-08 22:57
Viresh Shah18-Jul-08 22:57 
GeneralABS love it !!! Pin
izmoto2-Apr-08 1:44
izmoto2-Apr-08 1:44 
GeneralAnother way to do it Pin
Einar Egilsson15-Aug-07 7:14
Einar Egilsson15-Aug-07 7:14 
GeneralRe: Another way to do it Pin
nnm28-Aug-07 12:34
nnm28-Aug-07 12:34 
GeneralNICE, EXCELLENT!!!! Pin
hcihlen14-Aug-07 7:54
hcihlen14-Aug-07 7:54 
QuestionA Cleaner Way? Pin
sstreaker8-Jul-07 2:38
sstreaker8-Jul-07 2:38 
I came up with what I think is a cleaner way to do this, perhaps you will agree:

Instead of using the #if compiler conditional to test for debug mode, which is perfect except for (as you mentioned) having to switch between Debug/Release mode all the time, you can use a command-line argument. Set the command line argument to "debugmode" or something simliar on the Debug tab in your project properties, then test for the existence of this argument in Main. Now when you run the project from Visual Studio, the command line arg will be set and you'll always run the non-service code; outside of Visual Studio, your app will run only as a Windows service.


// The main entry point for the process
static void Main(string[] args) {

if(args.Length > 0 && args[0] == "debugmode") {

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

} else {

System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);

}

}
AnswerRe: A Cleaner Way? Pin
Lee Humphries8-Jul-07 13:37
professionalLee Humphries8-Jul-07 13:37 
GeneralI dont get it [modified] Pin
TEMoore29-May-07 11:57
TEMoore29-May-07 11:57 
GeneralRe: I dont get it Pin
Lee Humphries29-May-07 12:27
professionalLee Humphries29-May-07 12:27 
GeneralRe: I dont get it Pin
TEMoore30-May-07 5:21
TEMoore30-May-07 5:21 
GeneralRe: I dont get it Pin
Lee Humphries30-May-07 16:12
professionalLee Humphries30-May-07 16:12 
GeneralElegant Solution Pin
John Doherty15-May-07 8:16
John Doherty15-May-07 8:16 
GeneralTimer event isn't called Pin
Nicolas Stuardo10-Apr-07 11:13
Nicolas Stuardo10-Apr-07 11:13 
GeneralRe: Timer event isn't called Pin
Lee Humphries10-Apr-07 17:09
professionalLee Humphries10-Apr-07 17:09 
GeneralRe: Timer event isn't called Pin
Anderson Imes18-Apr-07 3:07
Anderson Imes18-Apr-07 3:07 
GeneralGreat piece of code Pin
jimcmt1-Mar-07 0:56
jimcmt1-Mar-07 0:56 
GeneralGood stuff...and so easy Pin
craig.w7-Feb-07 14:50
craig.w7-Feb-07 14:50 
GeneralGood Job! Pin
richan6-Jan-07 23:03
richan6-Jan-07 23:03 
JokeI stole your idea Pin
Anderson Imes28-Dec-06 3:10
Anderson Imes28-Dec-06 3:10 
GeneralRe: I stole your idea Pin
Lee Humphries28-Dec-06 11:09
professionalLee Humphries28-Dec-06 11:09 
GeneralRe: I stole your idea Pin
Anderson Imes28-Dec-06 12:57
Anderson Imes28-Dec-06 12:57 
GeneralDEBUG switch Pin
Gnuconcepts17-Nov-06 3:38
Gnuconcepts17-Nov-06 3:38 
GeneralRe: DEBUG switch Pin
Lee Humphries19-Nov-06 14:29
professionalLee Humphries19-Nov-06 14:29 

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.