Do a search some time on Google for Services and C#, and you'll come across a lot of ASP.NET references for web services. Not very useful for a beginner looking for info on Windows Services! I've had this very problem, so I decided to do it myself, the old-fashioned way. This article in not intended to contend with anyone else's article. It is simply a basic skeleton of a Windows Service, nothing more.
I know that in Visual Studio 2003, there was a template for a Windows Service, but I also remember it was rather obfuscated by Microsoft as far as templates go. Now that I use Visual C# 2005 Express Edition, there is no template for Windows Services. So, starting from there, I created one that works in .NET 2.0.
Here's a small example on how to build a Windows Service in C#.
First, load up your IDE of choice (mine is the free Visual C# 2005 Express Edition) and create an empty project. You can do this in VC# 2005 EE by clicking the menus: [File]->[New Project], Select "Empty Project", and name it what you will (mine is called WindowsService, original!). Click OK to create the project. Your project is created, and initially, it's not saved anywhere but a temporary location, so go ahead and go to [File]->[Save All], and okay through the dialog. This will officially save your project.
Next, add the
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
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:
class WindowsService : ServiceBase
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()
protected override void Dispose(bool disposing)
protected override void OnStart(string args)
protected override void OnStop()
protected override void OnPause()
protected override void OnContinue()
protected override void OnShutdown()
protected override void OnCustomCommand(int command)
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
protected override void OnSessionChange(
In the "WindowsServiceInstaller.cs" class, copy the following:
public class WindowsServiceInstaller : Installer
ServiceProcessInstaller 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";
ServiceAccount section of the code is important for the security context under which your code will run. For every enumeration except
User, set the
Password properties to
You can set the
Account property to the following (this is mostly from Microsoft's help file):
LocalSystem enumeration defines a highly privileged account, but most services do not require such an elevated privilege level.
LocalService enumeration provides a lower privilege level for the security context.
NetworkService enumeration provides extensive local privileges, and provides the computer's credentials to remote servers.
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.
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).
REM The following directory is for .NET 2.0
echo Installing WindowsService...
InstallUtil /i WindowsService.exe
Points of Interest
I have noticed that other examples on creating services (when I can find them) are more focused on everything *but* the actual creation and structure of a Windows service. Since I did this mainly for a work project, I had to learn this all on my own, which in itself is rewarding, but I can imagine how other people feel when they need this kind of code as soon as possible and don't have the time to research it. Hopefully, this will help others who need just the basics and can go from there.
I've been programming almost 21 years now, and have used more than 10 different languages in the past.