Click here to Skip to main content
16,020,519 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.9K   77   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

 
QuestionHave a doubt in myServ.Process() Pin
nirmalasudar18-Jun-13 19:13
nirmalasudar18-Jun-13 19:13 
AnswerRe: Have a doubt in myServ.Process() Pin
Tejas Vaishnav21-Jun-13 2:42
professionalTejas Vaishnav21-Jun-13 2:42 
Questionwindow service Pin
sudheeshkumar6-Mar-13 1:13
sudheeshkumar6-Mar-13 1:13 
AnswerRe: window service Pin
Tejas Vaishnav7-May-13 21:01
professionalTejas Vaishnav7-May-13 21:01 
GeneralMy vote of 5 Pin
Marcelo.sud27-Dec-12 9:00
Marcelo.sud27-Dec-12 9:00 
GeneralRe: My vote of 5 Pin
Tejas Vaishnav27-Dec-12 19:55
professionalTejas Vaishnav27-Dec-12 19:55 
QuestionStill geting the message after changin code Pin
Luis Fernando Forero Guzman19-Dec-12 4:54
Luis Fernando Forero Guzman19-Dec-12 4:54 
AnswerRe: Still geting the message after changin code Pin
Tejas Vaishnav27-Dec-12 19:56
professionalTejas Vaishnav27-Dec-12 19:56 
Sorry for late reply.

Are you talking about this tip or anything else?
Thanks & Regards
Tejas Vaishnav
Find me on Facebook | Blog

QuestionHow to Debug or Test your Windows Service Without Installing it Pin
Singh Vijay Kumar5-Dec-12 1:02
professionalSingh Vijay Kumar5-Dec-12 1:02 
AnswerRe: How to Debug or Test your Windows Service Without Installing it Pin
Tejas Vaishnav7-Dec-12 0:30
professionalTejas Vaishnav7-Dec-12 0:30 
GeneralMy vote of 5 Pin
noname201523-Nov-12 12:49
noname201523-Nov-12 12:49 
GeneralRe: My vote of 5 Pin
Tejas Vaishnav23-Nov-12 17:19
professionalTejas Vaishnav23-Nov-12 17:19 
QuestionVery nice Pin
Marchief22-Nov-12 20:41
Marchief22-Nov-12 20:41 
QuestionTry this very nice solution Pin
ishalem22-Nov-12 3:18
ishalem22-Nov-12 3:18 
GeneralMy vote of 4 Pin
wvd_vegt22-Nov-12 3:02
professionalwvd_vegt22-Nov-12 3:02 
QuestionEnvironment.UserInteractive Pin
sergiogarcianinja22-Nov-12 2:39
sergiogarcianinja22-Nov-12 2:39 
QuestionOhh Really ...!!!! It Works, Life Saver Pin
Strange_Pirate27-Sep-12 1:26
Strange_Pirate27-Sep-12 1:26 
AnswerRe: Ohh Really ...!!!! It Works, Life Saver Pin
Tejas Vaishnav23-Nov-12 17:20
professionalTejas Vaishnav23-Nov-12 17:20 
GeneralMy vote of 5 Pin
deeps3365-Sep-12 0:21
deeps3365-Sep-12 0:21 
GeneralRe: My vote of 5 Pin
Tejas Vaishnav5-Sep-12 20:57
professionalTejas Vaishnav5-Sep-12 20:57 
QuestionBetter / prettier solution Pin
Rob_III17-Aug-12 5:55
Rob_III17-Aug-12 5:55 
GeneralMy vote of 5 Pin
AbdullaMohammad17-Jul-12 4:14
AbdullaMohammad17-Jul-12 4:14 
GeneralRe: My vote of 5 Pin
Tejas Vaishnav17-Jul-12 18:32
professionalTejas Vaishnav17-Jul-12 18:32 
GeneralMy vote of 1 Pin
Mahalingam2510-Jul-12 1:53
Mahalingam2510-Jul-12 1:53 
GeneralRe: My vote of 1 Pin
Tejas Vaishnav10-Jul-12 18:40
professionalTejas Vaishnav10-Jul-12 18:40 

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.