Creating a windows service is easy but it might be a bit tricky specially for those who is new in developing such project, like me when I developed my first windows service before I have to use installutil by invoking such command
C:\WINDOWS\Microsoft.NET\Framework\{FrameworkVersion}\InstallUtil.exe C:\MyDemoService.exe
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 set up of project in the least amount of steps possible.
Now lets start by creating a Windows Service Project
Once it created, you can now do the coding by switching to code view
Now for our sample we will write items on 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.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Timers; namespace Demo_Windows_Service { public partial class Service1 : ServiceBase { Timer myTimer = new Timer(); public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { //Starting //Handle Elapsed Event myTimer.Elapsed += new ElapsedEventHandler(OnElapsedTime); //Set Interval to 1 Second myTimer.Interval = 5000; //Enable the Timer myTimer.Enabled = true; } protected override void OnStop() { } private void OnElapsedTime(object source, ElapsedEventArgs e) { myTimer.Enabled = false; //Perform Processing Here DoSomething(); myTimer.Enabled = true; } private void DoSomething() { string sSource; string sLog; string sEvent; sSource = "Demo Windows Service"; sLog = "Log Something : " + DateTime.Now.ToString(); sEvent = "Event Fired"; if (!EventLog.SourceExists(sSource)) EventLog.CreateEventSource(sSource, sLog); EventLog.WriteEntry(sSource, sEvent); EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Information, 100); } } }
Once finished add an installer in your service, this piece of configuration will be invoked by the setup project. So go to the Service1.cs [Design] and right-click on the open area, then choose “Add Installer”
Once done you will hae two objects to configure, which are serviceProcessInstaller1 and serviceInstaller1
Now lets configure serviceProcessInstaller1, this defines how the service will run under 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 each of them then here is a description of all:
- 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 AUTHORITYSYSTEM” – 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 AUTHORITYLOCAL SERVICE” – This is a built-in account that has the same privileges like the 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 AUTHORITYNETWORK 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 lets 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, Next thing to do now is your Setup Project, do that by adding a new project in your solution
Choose a” setup project” under “other project types”
Once chosen, you will be directed to 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 is the output of your project which can be an Executable File, Class Library or even third-party Dll’s, In our case it the class library.
Just choose the Service Project the Primary Output, click OK
Now it will display all outputs plus 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 had 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 you Installer description, author, manufacturer, installer behaviour etc. This is the one shown on the 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 have successfully installed.
You can also see programs and features, you can uninstall it from here.