Windows Service Test Form






3.90/5 (11 votes)
Jul 5, 2006
1 min read

81751

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