Create And Install Windows Service Step By Step In C#

Windows Service is a standalone application which is started automatically or manually as per setting and complete their specific task. We can handle service using start, restart and stop. It does not have any User Interface from where you can handle the service feature. It is created and hosted on the system.

So, we can say basically Windows Service is an application that run in background and work on various tasks. Some time it is started automatically when system boot and sometimes you need to start the service manually. All the Windows Service is controlled through the Service Control Manager.

Create the Windows Service, Open Visual Studio, from the File Menu, select New and then choose Project. It will open a New Project window where you can choose various type of the project.

Go to Visual C# and select Classic Desktop and from the next window, choose Windows Service. Name the service “MyFirstWindowsService” and click to OK.

MyFirstWindowsService

It will add a Windows Service for you. Rename the Service1.cs to TestService.cs.

Windows Service

See the following Program.cs code, it is responsible for running the Windows Service.

Program

Open TestService.cs [Design] mode and add a Timer control.

Timer control

Open TestService.cs and made the changes in the code as in the following. You need to write the logic to process some task on OnStart and OnStop.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Diagnostics;  
  6. using System.IO;  
  7. using System.Linq;  
  8. using System.ServiceProcess;  
  9. using System.Text;  
  10. using System.Threading.Tasks;  
  11. namespace MyFirstWindowsService  
  12. {  
  13.     public partial class TestService: ServiceBase  
  14.     {  
  15.         System.Timers.Timer timeDelay;  
  16.         int count;  
  17.         public TestService()  
  18.         {  
  19.             InitializeComponent();  
  20.             timeDelay = new System.Timers.Timer();  
  21.             timeDelay.Elapsed += new System.Timers.ElapsedEventHandler(WorkProcess);  
  22.         }  
  23.         public void WorkProcess(object sender, System.Timers.ElapsedEventArgs e)  
  24.         {  
  25.             string process = "Timer Tick " + count;  
  26.             LogService(process);  
  27.             count++;  
  28.         }  
  29.         protected override void OnStart(string[] args)  
  30.         {  
  31.             LogService("Service is Started");  
  32.             timeDelay.Enabled = true;  
  33.         }  
  34.         protected override void OnStop()  
  35.         {  
  36.             LogService("Service Stoped");  
  37.             timeDelay.Enabled = false;  
  38.         }  
  39.         private void LogService(string content)  
  40.         {  
  41.             FileStream fs = new FileStream(@ "d:\TestServiceLog.txt", FileMode.OpenOrCreate, FileAccess.Write);  
  42.             StreamWriter sw = new StreamWriter(fs);  
  43.             sw.BaseStream.Seek(0, SeekOrigin.End);  
  44.             sw.WriteLine(content);  
  45.             sw.Flush();  
  46.             sw.Close();  
  47.         }  
  48.     }  
  49. }  
Here in above code, you can see, service will start on OnStart and stop on OnStop method. You need to create logging system for logging the task to be completed in the process.

Now it is time to Add Installer to install the service. Go to TestService.cs Design View and right click on the screen and choose Add Installer. And provide the name as “ProjectInstaller”.

Add Installer

When you click on Add Installer, it will automatically add the ProjectInstaller.cs with two new components “ServiceProcessInstaller1” and “ServiceInstaller1”.

Go to property of the ServiceProcessInstaller1 and change the Account type as “Local System”.

ServiceProcessInstaller1

Now go to ServiceInstaller1 and choose the property option for this and change the ServieName for the service from the property.

ServiceInstaller1

Now build the project and go to the build location where you can find the .exe for the windows service.

So, we can install it.

build the project

For installing the Windows Service, Open Visual Studio Command Prompt as Administrator mode and go to the following location where your windows service has created service .exe file and here you can use InstallUtil.exe “MyFirstWindowsService.exe” for installing the service.

InstallUtil

To see the installing service, go to program and type Services.msc in search program. Here you can find your service “My First Windows Service” and in the left hand side you can see the Start menu from where you can start your service.

My First Windows Service

While starting the service, you will see two more options “Stop” and “Restart”.

start the service

Open the D:\TestServiceLog.txt to see the log process of the Windows Service.

TestServiceLog

When you click to stop the service, in the log file you can see the service has been stopped.

stop the service

Thanks for reading this article, hope you enjoyed. It.