Click here to Skip to main content
15,867,686 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 443.9K   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

 
QuestionDebug Windows Service Pin
Member 1328286930-Jun-19 4:15
Member 1328286930-Jun-19 4:15 
QuestionSTAThread may be needed Pin
B. Clay Shannon24-May-16 8:00
professionalB. Clay Shannon24-May-16 8:00 
QuestionHow to stop the service? Pin
B. Clay Shannon24-May-16 6:58
professionalB. Clay Shannon24-May-16 6:58 
So starting the service is as easy as hitting F5 from VS; how about stopping it? Shift+F5? There is no indication it's running (in the VS title bar), so how to know that it really stopped?

If I run the Service multiple times, are there multiple instances of the Service running still? If so, how do I "kill" them?
QuestionExplain Syntax Pin
IrishChieftain19-May-16 11:50
IrishChieftain19-May-16 11:50 
Questiona better solution PinPopular
יהושע אלפי23-Dec-15 5:29
יהושע אלפי23-Dec-15 5:29 
PraiseThanks for the article Pin
JEBA ANANTH VIJAYARAJAN25-Nov-15 23:25
JEBA ANANTH VIJAYARAJAN25-Nov-15 23:25 
GeneralRe: Thanks for the article Pin
Tejas Vaishnav26-Nov-15 0:27
professionalTejas Vaishnav26-Nov-15 0:27 
QuestionGreat! Pin
Atroc3-Sep-15 5:34
Atroc3-Sep-15 5:34 
AnswerRe: Great! Pin
Tejas Vaishnav3-Sep-15 19:49
professionalTejas Vaishnav3-Sep-15 19:49 
QuestionCould we Pin
nomidis12-Dec-14 2:11
nomidis12-Dec-14 2:11 
GeneralNice Trick Pin
granadaCoder21-Oct-14 10:51
granadaCoder21-Oct-14 10:51 
GeneralRe: Nice Trick Pin
Tejas Vaishnav29-Oct-14 19:44
professionalTejas Vaishnav29-Oct-14 19:44 
QuestionCopied article Pin
ursul_boa19-Nov-13 21:53
ursul_boa19-Nov-13 21:53 
AnswerRe: Copied article Pin
Tejas Vaishnav20-Nov-13 2:21
professionalTejas Vaishnav20-Nov-13 2:21 
Questiongood article Pin
Jayesh Goyani21-Oct-13 2:15
Jayesh Goyani21-Oct-13 2:15 
AnswerRe: good article Pin
Tejas Vaishnav21-Oct-13 2:46
professionalTejas Vaishnav21-Oct-13 2:46 
SuggestionMy vote of 1 and the reason why you should avoid such debugging/testing Pin
Lex Li5-Sep-13 22:29
professionalLex Li5-Sep-13 22:29 
GeneralRe: My vote of 1 and the reason why you should avoid such debugging/testing Pin
Tejas Vaishnav5-Sep-13 22:40
professionalTejas Vaishnav5-Sep-13 22:40 
GeneralMy vote of 1 Pin
Lex Li5-Sep-13 22:22
professionalLex Li5-Sep-13 22:22 
Questioncheated Pin
arshadssd23-Aug-13 3:22
arshadssd23-Aug-13 3:22 
AnswerRe: cheated Pin
Tejas Vaishnav25-Aug-13 20:10
professionalTejas Vaishnav25-Aug-13 20:10 
GeneralMy vote of 5 Pin
Member 76536939-Aug-13 12:41
Member 76536939-Aug-13 12:41 
GeneralRe: My vote of 5 Pin
Tejas Vaishnav11-Aug-13 18:44
professionalTejas Vaishnav11-Aug-13 18:44 
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 

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.