Create an Unattended Windows Service using .NET

A Windows Service is a process that runs
unattended
and doesn't have a user interface. Windows Services can run on WinNT, Win2K,
Windows Server 2003, and WinXP. Services can start when the operating system
boots and other applications can interact with them.

Services can perform functions such as logging, auditing, database connection
pooling, etc. This sample creates a service that logs events to a file. Events
originate in a sample application that simulates when gates in a security fence
open or close. The .NET solution contains two projects: the WindowsService
project which implements the service and the driver project that
utilizes it.

You can walk through the code which is straightforward. Here, I'll mention a few
important details.

To create a Windows Service project, select the Windows Service template
in the VS.NET IDE. I named the project AppMonitor. Open AppMonitor.VB in Designer
view and look at its properties. You can set properties telling Windows that
the service will handle shutdown, pause, continue, and other events. The
AutoLog property tells the service to automatically write to the
Event Log when the it starts and stops.

Viewing the code, you see the AppMonitor class inherits from the
System.ServiceProcess.ServiceBase namespace. Also there is an
handler for the various service events (OnStart, OnStop,
OnPause...). These handlers override those in the
System.ServiceProcess.ServiceBase class and allow the service to execute the
desired logic when it starts, stops, etc. The OnStart procedure takes a string
array parameter. You pass values into OnStart using the Services MMC snap-in in
Control Panel.

The OnCustomCommand event allows an application to
interact with a running service by passing in integer values. Code in
OnCustomCommand must interogate the value and execute the corresponding logic.
Integer values must be in the range 128 to 255. Interaction with a service is
primitive and one way, OnCustomCommand cannot return a value. Also, your
service has hard coded ties to the application(s) that use it.

The driver application let's you start and stop the service. If has a reference
to the System.ServiceProcess.dll and creates an instance of a
ServiceController which is used to control a service. To start or
stop a service you call the ServiceController's start or stop method then wait
for the service's status to change using the controller's WaitForStatus
method. Once the service has started you send integer commands to it using the
ServiceController's ExecuteCommand method.

Before you can run the code you must compile it and install the service. Build
the .NET solution then open the AppMonitor.VB in Designer view. Go to
properties and click the Add Installer link. This
adds a ProjectInstaller.VB class which updates the Windows Registry with
your service. To install the service you must run the InstallUtil
component described below.

Download the source code and compile it. Open a Visual Studio .NET 2003 Command
Prompt and navigate to \bin folder containing the compiled .EXE. Install the
service by typing InstallUtil AppMonitor.Exe into the command prompt and
hitting Enter (you may need to specify the complete path to InstallUtil which
is in the .NET Framework folder). Look at the log files create in the \bin
folder. Run the driver sample and step through the code. Examine the monitor.txt
log file created in the root folder.

Notes

This code will not run under version 1.0 of the .Net framework. You must use the
InstallUtil program from V1.1 of the Framework. To uninstall the service, use
the InstallUtil /u AppMonitor.exe command.