Creating a windows service in .NET

In this article, we will discuss how to create windows services in .net using a visual studio project template. We will also see how to install and uninstall the windows service in the system. You can also download the source code for the windows service detailed in this post.

Objectives:

What is Windows Service

Windows services are programs that run in the background and it does not need any user interaction. This kind of services is basically used for tasks that do not need any user input and runs periodically. Mean, It is useful when we need a task which runs in certain intervals such as, every 10 min, every hour.

Example of such windows services are like scanning of new email, windows update scan, event logger, SQL agent, Scanning of any folder for new files etc.

Windows services generally start when the operating system boots up. We can also set up startup mode as manual, automatic and delayed for these services at any point in time.

We can see the list of windows service by using either of the below commands:

  1. Open task manager, Check the Services tab.

TaskManager_List of service

2. Open Run. Write “Services.msc” and click OK.

ListOfServices

Creating Windows Service in .net:

  1.  Add new Project and select Windows Service.

WinServiceUsingTemplate

2.    It will add the services.cs files in the project.

3.   Now open Services.cs file, Right-click on empty space and select ‘AddInstaller’ from the context menu. This will add the Installer file to the project.

The installer file will be used to register services to the Service Control Manager.

Installer

4. We can add the update details like the ServiceName, ServiceDescription, Startup type and ServiceAccount etc in ProjectInstaller1.cs file. You can get the code file by right click on the page and click on “view code”.

UpdateDetails

 

5. In Service1.cs file, We will get 2 methods OnStart() and OnStop() for the task which executes on service startup and service stop event respectively.

Here, In this example, OnStart event, i am setting a timer that logs the message in every tick.

 

 

Now the service has been created.

Installation/Uninstallation of windows service: 

There is 2 ways to install this service :

1.  Using installutil :

  • Open VS Developer Command prompt.
  • Execute this command : installutil “Path/”<yourproject>.exe.

This command will install the window service to the system. You can see the service in the service list in Task Manager Or Services list as mentioned earlier.

Note: If you are using CMD(regular command prompt), you need to set the directory location where the InstallUtil.exe exist. InstallUtil.exe exist in the below path:

  • For 32 bit machine: C:\Windows\Microsoft.NET\Framework\v4.0.30319\
  • For 64 bit machine: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\

Uninstallation Command : installutil /u “Path/”<yourproject>.exe

2. Using SC Command:

  •  Open CMD as administrator
  •  Execute Command: SC Create “ServiceName” binPath=“Path/”<yourproject>.exe

Uninstallation Command : SC Delete “ServiceName”

Debugging windows service

Debugging windows service is not straight forward as a console application in visual studio.

To debug windows service below steps should be followed :

1.  Install the service and run :  

You can run the service from the Services list.

WindowServiceStart

2.  Attach the running process to the debugger.

Go to Debug -> Click on AttachProcess -> Select the running window service process.

AttachToProcess

After attaching the service process, it will start hit the breakpoints in the code.

Download Sourcecode : WinServiceSourceCode

Conclusion:

In this article, We have seen how to create a windows service with the visual studio template. We have also seen the installation and debugging process.

In the next article, We will see how does topself helps us to create windows service and simplify the installation and debugging process. Windows service using topshelf

Thanks.

Refereces : Microsoft doc

admin

Software professional, Blogger, Tech enthusiast.