Click here to Skip to main content
15,124,861 members
Articles / Web Development / ASP.NET
Posted 18 Apr 2005


181 bookmarked

Debugging Windows Services under Visual Studio .NET

Rate me:
Please Sign up or sign in to vote.
4.91/5 (115 votes)
14 Aug 2006CPOL1 min read
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 ;)). 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
Timor-Leste Timor-Leste
If it ain't broke - that can be arranged.

Comments and Discussions

GeneralRe: Very nice! Pin
Lee Humphries22-Aug-06 15:55
professionalLee Humphries22-Aug-06 15:55 
GeneralRe: Very nice! Pin
dferreira04222-Aug-06 16:01
Memberdferreira04222-Aug-06 16:01 
JokeRe: Very nice! Pin
Lee Humphries22-Aug-06 17:20
professionalLee Humphries22-Aug-06 17:20 
GeneralRe: Very nice! Pin
dferreira04222-Aug-06 17:40
Memberdferreira04222-Aug-06 17:40 
JokeRe: Very nice! Pin
Lee Humphries22-Aug-06 17:49
professionalLee Humphries22-Aug-06 17:49 
GeneralClever workaround! Pin
moriority511-Aug-06 12:05
Membermoriority511-Aug-06 12:05 
GeneralRe: Clever workaround! Pin
Lee Humphries13-Aug-06 12:21
professionalLee Humphries13-Aug-06 12:21 
GeneralSolution to that Pin
Leo Davidson15-Aug-06 3:18
MemberLeo Davidson15-Aug-06 3:18 
You can access the command-line arguments during the service startup (just change the "static void Main()" into "static void Main(string[] args)").

My service will run as a normal command-line app, similar to what yours does in a Debug build, if you give it the "/DEBUG" argument on the command-line. This lets me run it manually inside and outside of the debugger, whether I've done a release or a debug build, and not have to worry about shipping the wrong version either.

The only additional change is to edit the project settings to add "/DEBUG" to the command-line arguments for debugging.

GeneralThanks - And Some Extra Stuff Pin
jriesen27-Jun-06 21:31
Memberjriesen27-Jun-06 21:31 
GeneralRe: Thanks - And Some Extra Stuff Pin
Lee Humphries28-Jun-06 13:26
professionalLee Humphries28-Jun-06 13:26 
GeneralBest article !!! Pin
Phan Dung7-May-06 23:59
MemberPhan Dung7-May-06 23:59 
GeneralWorks great Pin
pchelp25-Apr-06 11:34
Memberpchelp25-Apr-06 11:34 
GeneralThanks Pin
Victtim5-Apr-06 11:22
MemberVicttim5-Apr-06 11:22 
QuestionHow about Pin
nfoyt30-Mar-06 14:18
Membernfoyt30-Mar-06 14:18 
AnswerRe: How about Pin
Lee Humphries30-Mar-06 14:33
professionalLee Humphries30-Mar-06 14:33 
GeneralAwesome!!! Pin
ajdiaz3-Mar-06 7:41
Memberajdiaz3-Mar-06 7:41 
Generalthanks - found this useful Pin
Jay Hamlin18-Nov-05 8:37
MemberJay Hamlin18-Nov-05 8:37 
GeneralCaveat ! Pin
tobia_p15-Sep-05 6:56
Membertobia_p15-Sep-05 6:56 
GeneralRe: Caveat ! Pin
Lee Humphries15-Sep-05 16:21
professionalLee Humphries15-Sep-05 16:21 
QuestionWhat is the gain? Pin
tgueth19-Apr-05 6:07
professionaltgueth19-Apr-05 6:07 
AnswerRe: What is the gain? Pin
Lee Humphries19-Apr-05 16:13
professionalLee Humphries19-Apr-05 16:13 

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.