Click here to Skip to main content
15,503,579 members
Articles / Programming Languages / C#
Tip/Trick
Posted 28 Sep 2011

Tagged as

Stats

415.7K views
76 bookmarked

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
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 5:15
Member 1328286930-Jun-19 5:15 
QuestionSTAThread may be needed Pin
B. Clay Shannon24-May-16 9:00
professionalB. Clay Shannon24-May-16 9:00 
QuestionHow to stop the service? Pin
B. Clay Shannon24-May-16 7:58
professionalB. Clay Shannon24-May-16 7:58 
QuestionExplain Syntax Pin
IrishChieftain19-May-16 12:50
IrishChieftain19-May-16 12:50 
Questiona better solution PinPopular
יהושע אלפי23-Dec-15 6:29
יהושע אלפי23-Dec-15 6:29 
PraiseThanks for the article Pin
JEBA ANANTH VIJAYARAJAN26-Nov-15 0:25
JEBA ANANTH VIJAYARAJAN26-Nov-15 0:25 
GeneralRe: Thanks for the article Pin
Tejas Vaishnav26-Nov-15 1:27
professionalTejas Vaishnav26-Nov-15 1:27 
QuestionGreat! Pin
Atroc3-Sep-15 6:34
Atroc3-Sep-15 6:34 
AnswerRe: Great! Pin
Tejas Vaishnav3-Sep-15 20:49
professionalTejas Vaishnav3-Sep-15 20:49 
QuestionCould we Pin
nomidis12-Dec-14 3:11
nomidis12-Dec-14 3:11 
GeneralNice Trick Pin
granadaCoder21-Oct-14 11:51
granadaCoder21-Oct-14 11:51 
GeneralRe: Nice Trick Pin
Tejas Vaishnav29-Oct-14 20:44
professionalTejas Vaishnav29-Oct-14 20:44 
Thanks Smile | :) Smile | :)
Tejas Vaishnav
Find me on Facebook | Blog

QuestionCopied article Pin
ursul_boa19-Nov-13 22:53
ursul_boa19-Nov-13 22:53 
AnswerRe: Copied article Pin
Tejas Vaishnav20-Nov-13 3:21
professionalTejas Vaishnav20-Nov-13 3:21 
Questiongood article Pin
Jayesh Goyani21-Oct-13 3:15
Jayesh Goyani21-Oct-13 3:15 
AnswerRe: good article Pin
Tejas Vaishnav21-Oct-13 3:46
professionalTejas Vaishnav21-Oct-13 3:46 
SuggestionMy vote of 1 and the reason why you should avoid such debugging/testing Pin
Lex Li5-Sep-13 23:29
professionalLex Li5-Sep-13 23:29 
GeneralRe: My vote of 1 and the reason why you should avoid such debugging/testing Pin
Tejas Vaishnav5-Sep-13 23:40
professionalTejas Vaishnav5-Sep-13 23:40 
GeneralMy vote of 1 Pin
Lex Li5-Sep-13 23:22
professionalLex Li5-Sep-13 23:22 
Questioncheated Pin
arshadssd23-Aug-13 4:22
arshadssd23-Aug-13 4:22 
AnswerRe: cheated Pin
Tejas Vaishnav25-Aug-13 21:10
professionalTejas Vaishnav25-Aug-13 21:10 
GeneralMy vote of 5 Pin
Member 76536939-Aug-13 13:41
Member 76536939-Aug-13 13:41 
GeneralRe: My vote of 5 Pin
Tejas Vaishnav11-Aug-13 19:44
professionalTejas Vaishnav11-Aug-13 19:44 
QuestionHave a doubt in myServ.Process() Pin
nirmalasudar18-Jun-13 20:13
nirmalasudar18-Jun-13 20:13 
AnswerRe: Have a doubt in myServ.Process() Pin
Tejas Vaishnav21-Jun-13 3:42
professionalTejas Vaishnav21-Jun-13 3: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.