BlackWaspTM
Windows Programming
.NET 1.1+

Creating a Windows Service (2)

Windows services are background processes that usually have no direct interaction with the user interface. This article explains how to create a windows service with an example that monitors the file system for changes using a FileSystemWatcher object.

Creating the Project

Windows Service IconTo begin, we need to create a project in Visual Studio using the Windows Service template. Start Visual Studio and specify that you wish to create a new project. Select this Windows Server option and name the project "FileMonitorService".

Once the new project is created, you will see that it contains a class named "Service1". This contains the basic functionality of the service. If you double-click the file you will see a design window. In a similar manner to when creating Windows forms applications, you can drag components from the toolbox into this designer and use them with your service.

To view the code in the Service1 class click the "click here to switch to code view" link on the designer's surface or right-click the file in the Solution Explorer and choose "View Code". You will see that the class file contains some methods. The code will vary according to the version of Visual Studio being used. However, it will include the following members:

  • Constructor. A constructor will be defined and will call to InitializeComponent. This is required to enable components added using the designer.
  • OnStart. This is called when the service is started, either manually via the MMC or automatically when the operating system is loaded.
  • OnStop. This method is called when the service is stopped.

Note that the class inherits from ServiceBase, which contains the methods required to correctly operate a Windows service.

Service1 is not a good name. Rename the class file from "Service1.cs" to "Monitor.cs" and change the class definition to match the following. NB: If the current definition includes the "partial" keyword, leave this in place and just change the name of the class.

public class Monitor : ServiceBase

Depending upon the version of Visual Studio that you are using, the base class may have been fully qualified before we changed it. So that the service still compiles, ensure the following using directive is present:

using System.ServiceProcess;

The service will still not compile at this point, as the name of the constructor is incorrect. Modify the declaration of the constructor as follows and then compile the service.

public Monitor()

NB: You may find that the code fails to compile because there is a reference to the Service1 class in the Main method. If so, simply change the name from "Service1" to "Monitor" and try compiling again.

ServicesToRun = new ServiceBase[] { new Monitor() };

Initialising the Service

Any initialisation that is required would usually be added in the constructor, following the call to InitializeComponent. In our example service, we need to check that an event source exists for logging events. As the service may execute under an account with limited privileges, it may not be possible to create the event source within the service. Additionally, on some operating systems it is not possible to create an event source unless the program is running as an administrator.

A typical solution is to create a separate program that creates the event source and run this as an administrator before the service is used. This is explained in the article, "Logging Messages in Event Logs". If you do not wish to read that article now, simply create a new console application, add the following code to the Main method and run the program once.

string eventSource = "File Monitor Service";
System.Diagnostics.EventLog.CreateEventSource(eventSource, "Application");

NB: If you are using Microsoft Vista or a more recent operating system, you will need to compile the program and run it as an administrator.

10 September 2008