This section makes use of the concepts learned so far and
shows how we can implement a simple Windows Service in .NET.
Creating the Windows Service
To create a new project in Visual Studio .NET 2003, select
C# as the language of your choice and then select Windows Service as the
project. Specify the name of the project and save the same. Note that when
implementing a Windows Service, you should have two classes, one is the Service
class and the other is the Service Controller class. Refer to the following
listings that contain the source code for both the custom Service and the
custom ServiceController classes.
Listing 1: The Sample Windows Service Class
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
namespace SampleWindowsService
{
public class SampleWindowsService: System.ServiceProcess.ServiceBase
{
StreamWriter streamWriter;
private System.ComponentModel.Container components = null;
public SampleWindowsService()
{
InitializeComponent();
}
static void Main()
{
System.ServiceProcess.ServiceBase[]ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{
new SampleWindowsService()
};
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Sample Service";
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
protected override void OnStart(string[]args)
{
streamWriter = new StreamWriter(new FileStream(
"C:\\SampleWindowsServiceLogger.txt", System.IO.FileMode.Append));
this.streamWriter.WriteLine("Starting Sample Windows Service at " +
DateTime.Now.ToString());
this.streamWriter.Flush();
this.streamWriter.Close();
}
protected override void OnStop()
{
streamWriter = new StreamWriter(new FileStream(
"C:\\SampleWindowsServiceLogger.txt", System.IO.FileMode.Append));
this.streamWriter.WriteLine("Stopping Sample Windows Service at " +
DateTime.Now.ToString());
this.streamWriter.Flush();
this.streamWriter.Close();
}
}
}
Listing 2: The Sample Windows Service Installer
Class
using System;
using System.Collections;
using System.ComponentModel;
using System.ServiceProcess;
using System.Configuration.Install;
namespace SampleWindowsService
{
[RunInstaller(true)]
public class SampleWindowsServiceInstaller:
System.Configuration.Install.Installer
{
private System.ComponentModel.Container components = null;
public SampleWindowsServiceInstaller()
{
InitializeComponent();
ServiceProcessInstaller spi = new ServiceProcessInstaller();
ServiceInstaller si = new ServiceInstaller();
si.DisplayName = "Sample Windows Service";
si.ServiceName = "Sample Windows Service";
si.StartType = ServiceStartMode.Automatic;
this.Installers.Add(si);
spi.Account = ServiceAccount.LocalSystem;
spi.Username = null;
spi.Password = null;
this.Installers.Add(spi);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
}
}
The Windows Service implemented in this article stores the time
of starting, stopping or resuming the service in a file C:\\SampleWindowsServiceLogger.txt
in the local file system. Make changes to implement the service that can suit
your needs.
Start, stop and resume the Windows Service
Go to Control Panel | Administrative Tools | Computer
Management and select the Services and Applications tab. Then locate your
installed service and click on Start to start the service. The rest is self
explanatory.
Installing the Windows Service
In order to install the Windows Service implemented above,
use the installutil .NET Framework command line utility. Specify the following
at the command prompt.
Installutil SampleWindowsServiceInstaller.exe
Un-Installing the Windows Service
In order to uninstall the Windows service, specify the
following at the command prompt:
Installutil /u SampleWindowsServiceInstaller.exe