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...

By , 22 Nov 2012
 

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
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionwindow servicemembersudheeshkumar6 Mar '13 - 1:13 
AnswerRe: window servicememberTejas Vaishnav7 May '13 - 21:01 
GeneralMy vote of 5memberMarcelo.sud27 Dec '12 - 9:00 
GeneralRe: My vote of 5memberTejas Vaishnav27 Dec '12 - 19:55 
QuestionStill geting the message after changin codememberLuis Fernando Forero Guzman19 Dec '12 - 4:54 
AnswerRe: Still geting the message after changin codememberTejas Vaishnav27 Dec '12 - 19:56 
QuestionHow to Debug or Test your Windows Service Without Installing itmembervijayksingh5 Dec '12 - 1:02 
AnswerRe: How to Debug or Test your Windows Service Without Installing itmemberTejas Vaishnav7 Dec '12 - 0:30 
GeneralMy vote of 5groupAbdias Software23 Nov '12 - 12:49 
GeneralRe: My vote of 5memberTejas Vaishnav23 Nov '12 - 17:19 
QuestionVery nicememberMadlilMart22 Nov '12 - 20:41 
QuestionTry this very nice solutionmemberishalem22 Nov '12 - 3:18 
GeneralMy vote of 4memberwvd_vegt22 Nov '12 - 3:02 
QuestionEnvironment.UserInteractivemembersergio_ykz22 Nov '12 - 2:39 
QuestionOhh Really ...!!!! It Works, Life SavermemberStrange_Pirate27 Sep '12 - 1:26 
AnswerRe: Ohh Really ...!!!! It Works, Life SavermemberTejas Vaishnav23 Nov '12 - 17:20 
GeneralMy vote of 5groupdeeps3365 Sep '12 - 0:21 
GeneralRe: My vote of 5memberTejas_Vaishnav5 Sep '12 - 20:57 
QuestionBetter / prettier solutionmemberRob_III17 Aug '12 - 5:55 
GeneralMy vote of 5membersamthec17 Jul '12 - 4:14 
GeneralRe: My vote of 5memberTejas_Vaishnav17 Jul '12 - 18:32 
GeneralMy vote of 1memberMahalingam2510 Jul '12 - 1:53 
Not clear
GeneralRe: My vote of 1memberTejas_Vaishnav10 Jul '12 - 18:40 
QuestionRe: My vote of 1memberChlorine Addict4 Oct '12 - 6:57 
AnswerRe: My vote of 1memberTejas Vaishnav4 Oct '12 - 18:55 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 22 Nov 2012
Article Copyright 2011 by Tejas Vaishnav
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid