Click here to Skip to main content
15,878,809 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 599.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

 
QuestionThanks! Pin
LDN20-May-16 22:17
LDN20-May-16 22:17 
QuestionGreat Article !! Pin
AnilKAgarwal25-Jun-15 23:38
AnilKAgarwal25-Jun-15 23:38 
GeneralGreat Article!! Pin
Member 1115878016-Oct-14 5:26
Member 1115878016-Oct-14 5:26 
QuestionIt doesn't work for existing windows service code Pin
Member 784108626-May-14 0:24
Member 784108626-May-14 0:24 
AnswerRe: It doesn't work for existing windows service code Pin
Lee Humphries26-May-14 12:01
professionalLee Humphries26-May-14 12:01 
GeneralMy vote of 5 Pin
Sagar Shivdas Raut13-Mar-14 2:44
Sagar Shivdas Raut13-Mar-14 2:44 
GeneralMy vote of 5 Pin
SharadGNair13-Aug-13 23:43
SharadGNair13-Aug-13 23:43 
QuestionI’m still getting the message after changing code. Pin
Luis Fernando Forero Guzman19-Dec-12 5:20
Luis Fernando Forero Guzman19-Dec-12 5:20 
AnswerRe: I’m still getting the message after changing code. Pin
Lee Humphries19-Dec-12 11:45
professionalLee Humphries19-Dec-12 11:45 
GeneralMy vote of 5 Pin
menarenprvn22-Oct-12 4:19
menarenprvn22-Oct-12 4:19 
QuestionVery nice - be careful of OnStart Pin
Ritchie Annand11-Sep-12 8:43
Ritchie Annand11-Sep-12 8:43 
QuestionHow to handle the Stop request Pin
Bernhard Hiller23-May-12 3:33
Bernhard Hiller23-May-12 3:33 
AnswerRe: How to handle the Stop request Pin
Lee Humphries23-May-12 13:07
professionalLee Humphries23-May-12 13:07 
GeneralMy vote of 5 Pin
David Rodriguez29-Apr-12 9:45
David Rodriguez29-Apr-12 9:45 
GeneralMy vote of 5 Pin
eka80824-Apr-12 4:50
eka80824-Apr-12 4:50 
QuestionSo Cool! Pin
Marlene Deyo20-Mar-12 4:18
Marlene Deyo20-Mar-12 4:18 
QuestionLife saver! Pin
Chris Maunder12-Oct-11 11:54
cofounderChris Maunder12-Oct-11 11:54 
AnswerRe: Life saver! Pin
kicker20129-Jul-12 2:57
kicker20129-Jul-12 2:57 
QuestionTry Windows Service Helper Pin
breakpoint20-Sep-11 16:44
breakpoint20-Sep-11 16:44 
GeneralSimple things are often the best! Pin
Bewilderbeeste21-Jul-11 21:48
Bewilderbeeste21-Jul-11 21:48 
GeneralMy vote of 5 Pin
DragonLord663-May-11 23:24
DragonLord663-May-11 23:24 
GeneralMy vote of 5 Pin
cable beach20-Apr-11 17:30
cable beach20-Apr-11 17:30 
GeneralDebugging Windows service in simple way Pin
Krishna Mohan Reddy N8-Apr-11 3:53
Krishna Mohan Reddy N8-Apr-11 3:53 
GeneralRe: Debugging Windows service in simple way Pin
Lee Humphries10-Apr-11 13:50
professionalLee Humphries10-Apr-11 13:50 
GeneralMy vote of 5 Pin
rohancragg6-Jan-11 23:11
rohancragg6-Jan-11 23:11 

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.