Click here to Skip to main content
16,017,907 members
Articles / Programming Languages / C#
Tip/Trick

How to Debug or Test your Windows Service Without Installing it...

Rate me:
Please Sign up or sign in to vote.
4.89/5 (61 votes)
22 Nov 2012CPOL 456.7K   76   55
Debug or test your Windows Service without installing it...

Introduction

When you develop a Windows Service and want to run or debug it, you get a message box with this message:

Cannot start service from the command line or a debugger.
A Windows Service must first be installed (using installutil.exe)
and then started with the ServerExplorer, Windows Services 
Administrative tool or the NET START command.

So for testing you have to first install it on your computer, but it is a long process and also boring because every time you make changes, you have to reinstall your service and test it again.

Solution

For debugging or testing your service without installing it, make changes in Program.cs like this.

C#
static class Program
{
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
	{ 
	     new MyService() 
	};
        ServiceBase.Run(ServicesToRun);
    }
}

Change it to:

C#
static class Program
{
    static void Main()
    {
        #if(!DEBUG)
           ServiceBase[] ServicesToRun;
           ServicesToRun = new ServiceBase[] 
	   { 
	        new MyService() 
	   };
           ServiceBase.Run(ServicesToRun);
         #else
           MyService myServ = new MyService();
           myServ.Process();
           // here Process is my Service function
           // that will run when my service onstart is call
           // you need to call your own method or function name here instead of Process();
         #endif
    }
}

After adding #if and #else to your main fuction, now when you press F5 or run your service, it will not show you the previous message and simply run, so attach a break point to your method which will be called by the service when it will start. With the use of this code, you can simply debug your service without installing it.

For this no need to add any extra using directive (like using System.Data or using System.IO) to your class file. It will simply as it is.

Enjoy!!!

License

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


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: My vote of 1 Pin
Tejas Vaishnav4-Oct-12 18:58
professionalTejas Vaishnav4-Oct-12 18:58 
GeneralReason for my vote of 5 Thanks Tejas for the tip. This is in... Pin
Ravinder Singh Yadav10-Oct-11 18:56
Ravinder Singh Yadav10-Oct-11 18:56 
GeneralRe: thanks Pin
Tejas Vaishnav11-Oct-11 0:20
professionalTejas Vaishnav11-Oct-11 0:20 
GeneralThanks Tejas for the tip. This is indeed very helpful for so... Pin
Ravinder Singh Yadav10-Oct-11 18:55
Ravinder Singh Yadav10-Oct-11 18:55 
GeneralRe: Thanks Tejas for the tip. This is indeed very helpful for so... Pin
Tejas Vaishnav11-Jul-12 3:04
professionalTejas Vaishnav11-Jul-12 3:04 

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.