Click here to Skip to main content
11,429,977 members (76,621 online)
Click here to Skip to main content

Tagged as

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

, 22 Nov 2012 CPOL
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)

Share

About the Author

Tejas Vaishnav
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
QuestionCould we Pin
nomidis12-Dec-14 3:11
membernomidis12-Dec-14 3:11 
GeneralNice Trick Pin
granadaCoder21-Oct-14 11:51
membergranadaCoder21-Oct-14 11:51 
GeneralRe: Nice Trick Pin
Tejas Vaishnav29-Oct-14 20:44
professionalTejas Vaishnav29-Oct-14 20:44 
QuestionCopied article Pin
ursul_boa19-Nov-13 22:53
memberursul_boa19-Nov-13 22:53 
AnswerRe: Copied article Pin
Tejas Vaishnav20-Nov-13 3:21
memberTejas Vaishnav20-Nov-13 3:21 
Questiongood article Pin
Jayesh Goyani21-Oct-13 3:15
memberJayesh Goyani21-Oct-13 3:15 
AnswerRe: good article Pin
Tejas Vaishnav21-Oct-13 3:46
memberTejas 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
memberLex 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
memberTejas Vaishnav5-Sep-13 23:40 
GeneralMy vote of 1 Pin
Lex Li5-Sep-13 23:22
memberLex Li5-Sep-13 23:22 
Questioncheated Pin
arshadssd23-Aug-13 4:22
memberarshadssd23-Aug-13 4:22 
AnswerRe: cheated Pin
Tejas Vaishnav25-Aug-13 21:10
memberTejas Vaishnav25-Aug-13 21:10 
GeneralMy vote of 5 Pin
Member 76536939-Aug-13 13:41
memberMember 76536939-Aug-13 13:41 
GeneralRe: My vote of 5 Pin
Tejas Vaishnav11-Aug-13 19:44
memberTejas Vaishnav11-Aug-13 19:44 
QuestionHave a doubt in myServ.Process() Pin
nirmalasudar18-Jun-13 20:13
membernirmalasudar18-Jun-13 20:13 
AnswerRe: Have a doubt in myServ.Process() Pin
Tejas Vaishnav21-Jun-13 3:42
memberTejas Vaishnav21-Jun-13 3:42 
Questionwindow service Pin
sudheeshkumar6-Mar-13 2:13
membersudheeshkumar6-Mar-13 2:13 
AnswerRe: window service Pin
Tejas Vaishnav7-May-13 22:01
memberTejas Vaishnav7-May-13 22:01 
GeneralMy vote of 5 Pin
Marcelo.sud27-Dec-12 10:00
memberMarcelo.sud27-Dec-12 10:00 
GeneralRe: My vote of 5 Pin
Tejas Vaishnav27-Dec-12 20:55
memberTejas Vaishnav27-Dec-12 20:55 
QuestionStill geting the message after changin code Pin
Luis Fernando Forero Guzman19-Dec-12 5:54
memberLuis Fernando Forero Guzman19-Dec-12 5:54 
AnswerRe: Still geting the message after changin code Pin
Tejas Vaishnav27-Dec-12 20:56
memberTejas Vaishnav27-Dec-12 20:56 
QuestionHow to Debug or Test your Windows Service Without Installing it Pin
vijayksingh5-Dec-12 2:02
membervijayksingh5-Dec-12 2:02 
AnswerRe: How to Debug or Test your Windows Service Without Installing it Pin
Tejas Vaishnav7-Dec-12 1:30
memberTejas Vaishnav7-Dec-12 1:30 
GeneralMy vote of 5 Pin
Abdias Software23-Nov-12 13:49
groupAbdias Software23-Nov-12 13:49 
GeneralRe: My vote of 5 Pin
Tejas Vaishnav23-Nov-12 18:19
memberTejas Vaishnav23-Nov-12 18:19 
QuestionVery nice Pin
MadlilMart22-Nov-12 21:41
memberMadlilMart22-Nov-12 21:41 
QuestionTry this very nice solution Pin
ishalem22-Nov-12 4:18
memberishalem22-Nov-12 4:18 
GeneralMy vote of 4 Pin
wvd_vegt22-Nov-12 4:02
memberwvd_vegt22-Nov-12 4:02 
QuestionEnvironment.UserInteractive Pin
sergio_ykz22-Nov-12 3:39
membersergio_ykz22-Nov-12 3:39 
QuestionOhh Really ...!!!! It Works, Life Saver Pin
Strange_Pirate27-Sep-12 2:26
memberStrange_Pirate27-Sep-12 2:26 
AnswerRe: Ohh Really ...!!!! It Works, Life Saver Pin
Tejas Vaishnav23-Nov-12 18:20
memberTejas Vaishnav23-Nov-12 18:20 
GeneralMy vote of 5 Pin
deeps3365-Sep-12 1:21
groupdeeps3365-Sep-12 1:21 
GeneralRe: My vote of 5 Pin
Tejas_Vaishnav5-Sep-12 21:57
memberTejas_Vaishnav5-Sep-12 21:57 
QuestionBetter / prettier solution Pin
Rob_III17-Aug-12 6:55
memberRob_III17-Aug-12 6:55 
GeneralMy vote of 5 Pin
samthec17-Jul-12 5:14
membersamthec17-Jul-12 5:14 
GeneralRe: My vote of 5 Pin
Tejas_Vaishnav17-Jul-12 19:32
memberTejas_Vaishnav17-Jul-12 19:32 
thanks samthec
Thanks & Regards
Tejas Vaishnav
Find me on Facebook | Blog

GeneralMy vote of 1 Pin
Mahalingam2510-Jul-12 2:53
memberMahalingam2510-Jul-12 2:53 
GeneralRe: My vote of 1 Pin
Tejas_Vaishnav10-Jul-12 19:40
memberTejas_Vaishnav10-Jul-12 19:40 
QuestionRe: My vote of 1 Pin
Chlorine Addict4-Oct-12 7:57
memberChlorine Addict4-Oct-12 7:57 
AnswerRe: My vote of 1 Pin
Tejas Vaishnav4-Oct-12 19:55
memberTejas Vaishnav4-Oct-12 19:55 
AnswerRe: My vote of 1 Pin
Tejas Vaishnav4-Oct-12 19:58
memberTejas Vaishnav4-Oct-12 19:58 
GeneralReason for my vote of 5 Thanks Tejas for the tip. This is in... Pin
Ravinder Singh, PMP10-Oct-11 19:56
memberRavinder Singh, PMP10-Oct-11 19:56 
GeneralRe: thanks Pin
Tejas_Vaishnav11-Oct-11 1:20
memberTejas_Vaishnav11-Oct-11 1:20 
GeneralThanks Tejas for the tip. This is indeed very helpful for so... Pin
Ravinder Singh, PMP10-Oct-11 19:55
memberRavinder Singh, PMP10-Oct-11 19:55 
GeneralRe: Thanks Tejas for the tip. This is indeed very helpful for so... Pin
Tejas_Vaishnav11-Jul-12 4:04
memberTejas_Vaishnav11-Jul-12 4:04 

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 | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 22 Nov 2012
Article Copyright 2011 by Tejas Vaishnav
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid