Click here to Skip to main content
Click here to Skip to main content

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

, 22 Nov 2012
Rate this:
Please Sign up or sign in to vote.
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.

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

Change it to:

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)

About the Author

Tejas Vaishnav
Software Developer Priya Softwebsolutions
India India
No Biography provided

Comments and Discussions

 
QuestionCopied article Pinmemberursul_boa19-Nov-13 21:53 
AnswerRe: Copied article PinmemberTejas Vaishnav20-Nov-13 2:21 
Questiongood article PinmemberJayesh Goyani21-Oct-13 2:15 
AnswerRe: good article PinmemberTejas Vaishnav21-Oct-13 2:46 
SuggestionMy vote of 1 and the reason why you should avoid such debugging/testing PinmemberLex Li5-Sep-13 22:29 
GeneralRe: My vote of 1 and the reason why you should avoid such debugging/testing PinmemberTejas Vaishnav5-Sep-13 22:40 
GeneralMy vote of 1 PinmemberLex Li5-Sep-13 22:22 
Questioncheated Pinmemberarshadssd23-Aug-13 3:22 
AnswerRe: cheated PinmemberTejas Vaishnav25-Aug-13 20:10 
GeneralMy vote of 5 PinmemberMember 76536939-Aug-13 12:41 
GeneralRe: My vote of 5 PinmemberTejas Vaishnav11-Aug-13 18:44 
QuestionHave a doubt in myServ.Process() Pinmembernirmalasudar18-Jun-13 19:13 
AnswerRe: Have a doubt in myServ.Process() PinmemberTejas Vaishnav21-Jun-13 2:42 
Questionwindow service Pinmembersudheeshkumar6-Mar-13 1:13 
AnswerRe: window service PinmemberTejas Vaishnav7-May-13 21:01 
GeneralMy vote of 5 PinmemberMarcelo.sud27-Dec-12 9:00 
GeneralRe: My vote of 5 PinmemberTejas Vaishnav27-Dec-12 19:55 
QuestionStill geting the message after changin code PinmemberLuis Fernando Forero Guzman19-Dec-12 4:54 
AnswerRe: Still geting the message after changin code PinmemberTejas Vaishnav27-Dec-12 19:56 
QuestionHow to Debug or Test your Windows Service Without Installing it Pinmembervijayksingh5-Dec-12 1:02 
AnswerRe: How to Debug or Test your Windows Service Without Installing it PinmemberTejas Vaishnav7-Dec-12 0:30 
GeneralMy vote of 5 PingroupAbdias Software23-Nov-12 12:49 
GeneralRe: My vote of 5 PinmemberTejas Vaishnav23-Nov-12 17:19 
QuestionVery nice PinmemberMadlilMart22-Nov-12 20:41 
QuestionTry this very nice solution Pinmemberishalem22-Nov-12 3:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 22 Nov 2012
Article Copyright 2011 by Tejas Vaishnav
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid