Create Windows ServiceAdd the
System.ServiceProcess
and
System.Configuration.Install
references to your project. In the Solution Explorer, right-click "References", and select "Add Reference...". In the dialog box that pops up, make sure the ".NET" tab is selected, and then scroll down in the list to "
System.ServiceProcess
", select it, and click OK. You'll see a new entry under References. This will allow you to write code that references the
System.ServiceProcess
classes. Repeat the process to add the
System.Configuration.Install
reference.
Now, add two new class files to the project, one labeled: "WindowsService.cs" and the other: "WindowsServiceInstaller.cs". In the Solution Explorer, right-click the project name and go to: [Add]->[Class]. Name the class "WindowsService.cs" and then hit OK. Repeat the process for "WindowsServiceInstaller.cs". Now you have two brand-new files with the basics.
In the "WindowsService.cs" class, copy the following:
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
using System;
using System.Diagnostics;
using System.ServiceProcess;
namespace WindowsService
{
class WindowsService : ServiceBase
{
public WindowsService()
{
this.ServiceName = "My Windows Service";
this.EventLog.Log = "Application";
this.CanHandlePowerEvent = true;
this.CanHandleSessionChangeEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.CanStop = true;
}
static void Main()
{
ServiceBase.Run(new WindowsService());
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
}
protected override void OnStop()
{
base.OnStop();
}
protected override void OnPause()
{
base.OnPause();
}
protected override void OnContinue()
{
base.OnContinue();
}
protected override void OnShutdown()
{
base.OnShutdown();
}
protected override void OnCustomCommand(int command)
{
base.OnCustomCommand(command);
}
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
return base.OnPowerEvent(powerStatus);
}
protected override void OnSessionChange(
SessionChangeDescription changeDescription)
{
base.OnSessionChange(changeDescription);
}
}
}
In the "WindowsServiceInstaller.cs" class, copy the following:
using System;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace WindowsService
{
[RunInstaller(true)]
public class WindowsServiceInstaller : Installer
{
public WindowsServiceInstaller()
{
ServiceProcessInstaller serviceProcessInstaller =
new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Password = null;
serviceInstaller.DisplayName = "My New C# Windows Service";
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "My Windows Service";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}
}
The ServiceAccount
section of the code is important for the security context under which your code will run. For every enumeration except User
, set the Username
and Password
properties to null
.
You can set the Account
property to the following (this is mostly from Microsoft's help file):
- The
LocalSystem
enumeration defines a highly privileged account, but most services do not require such an elevated privilege level.
- The
LocalService
enumeration provides a lower privilege level for the security context.
- The
NetworkService
enumeration provides extensive local privileges, and provides the computer's credentials to remote servers.
- The
User
enumeration allows the use of a username and password combination to set the privilege level for the security context to that of a specified user.
There. That wasn't so tough! Now, in the WindowsService
class, depending on what you're creating this service for, you'll probably want to include a background working thread to handle, well, the work. I've used this code simply by leaving everything as-is with just the OnCustomCommand
method modified, and it works OK. If you wish to handle constant or timed processing, best add at least one working thread.
That's as far as I'll go in this article on setting up a Windows Service: everything else is pretty much up to the programmer. Now, the easy/hard part: Installation.
Installation
To install a service, you could create an installation program that encapsulates the executable for deployment, which I find time consuming and inefficient when testing an application. Or, you could install the service though the InstallUtil.exe process that comes with the .NET Framework.
I created a simple batch file, install.bat, to simplify installation (and therefore testing) of the service. The batch script is shown below; uninstallation is as simple as creating another batch file, uninstall.bat, with exactly the same script, only substituting the /i (for install) with /u (for uninstall).
@ECHO OFF
REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%
echo Installing WindowsService...
echo ---------------------------------------------------
InstallUtil /i WindowsService.exe
echo ---------------------------------------------------
echo Done.