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

Windows Service Test Form

, 5 Jul 2006
Rate this:
Please Sign up or sign in to vote.
An article on service debugging in VS2005.

Introduction

There are a few articles on the internet on debugging Windows Services in VS 2005. The official method is to attach a debugger to the service process once it has started. This makes it difficult to step through a service's startup process. The article, Debugging Windows Services under Visual Studio .NET, by Lee Humphries shows how to isolate individual methods using the DEBUG preprocessor directive. Here, we go a step further and create a test form as a sort of "service sandbox", to allow more interaction.

The ServiceTestForm keeps its own thread going, sort of like the system service manager. The difference here is that your service may not run as a system user. EventLog may not behave as expected either. It would be wise to use the static EventLog.WriteEntry instead of your EventLog instance.

Using the code

The code uses Anthony Roach’s C# Scheduler service as an example. To apply your own service, copy the embedded class ServiceTestForm into your class, and use the Main below. The form allows you to set breakpoints anywhere, including custom commands. Remember that command IDs below 128 are reserved for system commands.

static void Main()
{
   #if DEBUG
      System.Windows.Forms.Application.Run(
         new ServiceTestForm(new ScheduleService()));
   #else
      System.ServiceProcess.ServiceBase.Run(
         new System.ServiceProcess.ServiceBase[] { new ScheduleService() });
   #endif
}

Points of interest

At first, I thought it would be nice to override ServiceBase.EventLog properties in another preprocessor directive. Maybe it could redirect EventLog entries to the TextBox, but it turns out EventLog.WriteLine isn't virtual. If anybody has a better solution to this, please forumize it and Zip me an email.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

John Boero
Founder
United States United States
John Boero is a C# and graphics hobbyist. Some discoveries are too good to be kept closed, and deserve to be given back to the open community that has provided so much already.

Comments and Discussions

 
GeneralMoving ServiceTestForm outside the service class PinmemberMike Lang31-Aug-07 11:41 
GeneralAutomated Interface, rather than a one-off PinmemberAnderson Imes22-Aug-07 14:56 
QuestionWhy not to use ServiceController? PinmemberHDV18-Jul-06 22:22 
AnswerRe: Why not to use ServiceController? PinmemberHDV18-Jul-06 23:35 
GeneralYup... PinmemberJohn Boero19-Jul-06 4:08 
GeneralThanks! PinmemberHerman Chelette11-Jul-06 3:14 
Generalexcellent Pinmemberc4tes7-Jul-06 6:02 

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
Web04 | 2.8.141223.1 | Last Updated 5 Jul 2006
Article Copyright 2006 by John Boero
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid