Creating a Windows Service is easy, but it might be a bit tricky especially for those who are new to developing such a project, like me when I developed my first Windows Service. Before, I had to use installutil by invoking such a command:
Also, the Setup project wizard in Visual Studio is not that straightforward, so if you are looking for a step by step walk-through on how to achieve this task, then look no further as I will explain everything from development to the set up of a project in the least amount of steps possible.
Now let's start by creating a Windows Service Project:
Once it is created, you can now do the coding by switching to the code view:
Now for our sample, we will write items on an event log every 5 seconds; this makes sure that we know that the service is running as expected. So copy the code below to your project:
public partial class Service1 : ServiceBase
Timer myTimer = new Timer();
protected override void OnStart(string args)
myTimer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
myTimer.Interval = 5000;
myTimer.Enabled = true;
protected override void OnStop()
private void OnElapsedTime(object source, ElapsedEventArgs e)
myTimer.Enabled = false;
myTimer.Enabled = true;
private void DoSomething()
sSource = "Demo Windows Service";
sLog = "Log Something : " + DateTime.Now.ToString();
sEvent = "Event Fired";
Once finished, add an installer to your service; this piece of configuration will be invoked by the setup project. So go to Service1.cs [Design] and right-click on the open area, then choose “Add Installer”.
Once done, you will have two objects to configure, which are
Now let's configure
serviceProcessInstaller1. This defines who the service will run as. These are the things you see in the Logon tab of the service property. Now you can choose whether you want it to be User, LocalSystem, NetworkService, or LocalService.
If you don’t know the difference between them, then here is a description:
- User - This can be a domain user or a local user account, these are non built-in accounts that are given rights by an Administrator, so access rights might vary, so check with your administrator.
- Local System or “NT AUTHORITY\SYSTEM” – This is a built-in account with a very high-privilege on the local system, and it acts as the computer on the network.
- Local Service or “NT AUTHORITY\LOCAL SERVICE” – This is a built-in account that has the same privilege as members of the Users group, which means it has limited access. This account accesses the network resources as a null session without credentials.
- Network Service or “NT AUTHORITY\NETWORK SERVICE” - This is a built-in account that has more privileges than the members of the Users group. This account accesses the network resources by using the credentials of the computer account.
Now let's configure
serviceInstaller1. This section defines how the service will behave and it will also contain the description and name of the service you are setting up. These are the things you see in the General tab of the service property.
Now you are done with your installer. The next thing to do now is your Setup project; do that by adding a new project in your solution.
Choose 'Setup Project' under “Other project types”.
Once chosen, you will be directed to the file system editor; if it does not happen, you can always right-click the project, then choose “View” -> “File System”. Once in the File System Properties, choose Application Folder and right-click on it to add a “Project Output”. This is where you define what the output of your project is, which can be an executable file, Class Library, or even a third-party DLL. In our case, it is a Class Library.
Just choose the Service Project as the Primary Output, click OK.
Now it will display all the output plus the dependencies if you have any on your Windows Service Project.
Now choose Custom Actions; this defines what is copied on Install, Commit, Rollback, and Uninstall.
Choose the items you defined on the File System Properties by going to “Application Folder”.
Then choose the Primary Output of your Service:
Now it will show you that they are now configured on Install, Commit, Rollback, and Uninstall.
Now you are left with the properties; you can place your Installer description, author, manufacturer, installer behaviour etc. This is the what is shown on Add Remove Programs:
Now build your solution, then build your setup project. Once done, go to the Setup project folder and copy your setup to where you want to install your Windows Service.
Now run the setup and just follow the wizard:
Once finished, you are all good to go; type services.msc on the Run command to see if your service was successfully installed.
You can also see Programs and Features, you can uninstall from there.