Creating a Windows Service | How to add a Service into Services.msc in Windows?

 Today we will be looking at how to add a Service into list of windows services or in other words make our service managed by Service Control Manager(SCM).We can see the list of existing Windows Services by typing in the command "services.msc" in the Run window. This will open up a window listing the various services in the system currently and also there current state.

Suggested Reads : dotnet publish

Steps to Create Windows Service

We can create a Windows Service by using the Microsoft Visual Studio Project Template "Windows Service". By selecting the template along with the Target framework new Windows Service is Created.

Next Step is to Add a Project Installer to this Service to install the required components so that the Service Control Manager can know that a new service has been added. We can install the Project installer by right clicking on the designer file of the Service and choosing  "Add Installer" option, as shown in the figure below:

Add Service Installer

This will add new file called project Installer to the Service Project as shown below:

Project Installer

This contains all the Service Configuration information such as the Service name, Service Account credentials such as the Username and the password etc.

We can modify the service name if we want else we can leave the file untouched.

Then we can write our Service which is the main part doing the operation. This is similar to the WCF service. Once the service is written we need to host the service in Service Host as shown in the code below:


Service Host

 As you can see that we are inheriting the Service from ServiceBase class which provides two life cycle APIs such as the OnStart and OnStop. These will be called when the service is started by the SCM and Stopped by the SCM respectively.

We are hosting our Service Processor inside the ServiceHost as you can see from the API OnStart. Once we are done with the service side changes next thing is to add this service in the Windows Services list. This can be done as described below:

  • Open the Visual Studio  Developer command Prompt in Administrator mode.
  • Navigate to the path where the exe "InstallUtil.exe" is there, This is a .NET Utility which is used to register the service with the SCM so that it will be visible in the Windows Services List.
  • Type in the command "installutil <your Service Assembly name>.exe". Make sure that the path to your service assembly is proper.
  • Once its done successfully you will get a success message as the service is Installed Successfully and you will be able to see your service in the Windows Services List.
If you encounter any error during running this command, It can be easily debugged, This can be the topic for the Next Post.

Comments

Popular posts from this blog

Creating RESTful Minimal WebAPI in .Net 6 in an Easy Manner! | FastEndpoints

Mastering Concurrency with Latches and Barriers in C++20: A Practical Guide for Students

Graph Visualization using MSAGL with Examples