A Simple C# Windows Service

Several .NET facilities simplify the process of defining a Windows Service. This lab note steps through this process, defining a Windows Service in C#.

Step 1. Creating the Visual C# Solution and Project

These examples illustrate the creation and testing of a Windows Service written in C# using the Microsoft Visual C# 2008 Express Edition development environment. After launching Visual C# 2008 Express Edition, select the New Project item from the File menu. The New Project dialog opens. Here, select "Empty Project" from the list of installed templates. Note that other editions of Visual C# may include a Windows Service template. Since Visual C# 2008 Express Edition does not, we will demonstrate how to create a Windows Service from an Empty Project template. Here, we chose the name "k3" as our project. We will alter that name and preserve "k3" only as the name of the solution as we progress further.

After clicking "OK", the new "k3" solution and project appear in the Solution Explorer. In the image below, I've superimposed a Windows Explorer window to illustrate that no "k3" folder has yet been created in the Projects directory.

After selecting the "Save k3" item from the File menu, the directory structure for the "k3" solution and project are created as shown below.

We want to use "k3" as a generic solution name and potentially contain several projects. We rename the "k3" project to "kserver" by right-clicking on the "k3" project icon in the Solution Explorer and selecting the Rename menu item. The project name becomes an edit field and we enter "kserver". Note in the image below that Visual C# automatically changes the k3.csproj and k3.csproj.user files to kserver.csproj and kserver.csproj.user respectively.

Step 2. Adding the C# Classes

We will create two source files, each defining one class. The first file will define our "kserver" Windows Service. The second file will define a "kserverInstaller" class, which will be called by the InstallUtil utility to install and/or uninstall the kserver Windows Service.

To add the first source file, right-click on the "kserver" project and select the "New Item..." item from the "Add" menu. This will open the "Add New Item" dialog. Select the "Class" template type and name the new file "kserver.cs".

Clicking the "Add" button on the Add New Item dialog will cause the new kserver.cs file to be created. Also, several References are added to the kserver project, "System", "System.Data" and "System.Xml". We will not need all of these references. We will also edit the automatically generated source code placed within kserver.cs.

The image below shows the code we substitute for the default kserver.cs. Note that we need to add the System.ServiceProcess reference in order for Visual C# to compile this source file. To do that, we right-click on the "References" folder for the "kserver" project and select the "Add Reference ..." menu item. This opens a dialog from which we select the "System.ServiceProcess" reference. The "System.Data" and "System.Xml" references are not used, so we have removed them.

In the "kserver.cs" file, we declare our "kserver" class as a child of the "ServiceBase" class. This instruments kserver with all the facilities required to operate as a Windows Service. We must define a static "Main" function to serve as the program entry point. This Main function instantiates an instance of the kserver class as passes that instance to the ServiceBase.Run method. The other method defined is the kserver constructor. Here, we must provide a name for the service in the "ServiceName" member. Optionally, we have set the CanPauseAndContinue member to true.

We next create our second source file, "kserverInstaller.cs", using the same process we followed for kserver.cs. The image below shows the source code for this file. Note that we again need to adjust the kserver project references. In this case, we need to find and add the System.Configuration.Install reference.

In the kserverInstaller.cs file, we define the kserverInstaller class as a subclass of the Installer class. We also must prepend the class declaration with "[RunInstallerAttribute(true)]". The kserverInstaller class contains only two private members and the constructor method. In the constructor, we instantiate the ServiceInstaller and ServiceProcessInstaller members, set the process installer account to LocalSystem, set the service installer type and name, and append each installer to the installer collection. Note that the ServiceName member of the service installer must match the ServiceName member of the kserver class that we set in kserver.cs.

The kserver Windows Server is now ready to be built. However, we will add one more item to the project, an Assembly Information File. To do this, again right-click on the "kserver" project and select the "New Item ..." item from the "Add" menu. Select the "Assembly Information File" template and set the name of the file to "kserverAssemblyInfo.cs".

The new Assembly Info member will provide a lot of information my default. Here, we edit AssemblyTitle, AssemblyDescription, AssemblyCompany, and other fields as appropriate and rebuild the application.

Step 3. Installing the Windows Service

The "installutil" utility is used to install and uninstall a Windows Service. Here, for simplicity we copy the kserver.exe to "C:\" prior to running the utility. Run the installutil program with the new service executable name to install. Run the installutil program with the "/u" argument and the service executable name to uninstall.

After installing the service, the service is visible using the Services tool under Administrative Tools menu of the Control Panel. Our new service can be started, paused, resumed and stopped.

We have shown how to create a Windows Service in C# using .NET with Visual C# 2008 Express Edition.