Creating a Windows Service in C#

Introduction

Creating a Windows service in C# is as easy as other projects in Visual Studio. This blog describes what Windows service is and provides step-by-step in c# how to create a Windows service.

What is a Windows Service?

A Microsoft Windows service enables us to create long-running executable applications that run in the background with its own windows session. It can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface. It is also formerly known as NT service.

Getting Started

Microsoft has provided a project template called "Windows Service" in Visual Studio any version, you can use that template to create a windows service. This template automatically does much of the work for you by referencing the appropriate classes and namespaces, setting up the inheritance from the base class for services, and overriding several of the methods you're likely to want to override. Follow the below step to create a Windows service.

Steps to Create Windows Service

  1. Open Microsoft visual studio, go to the “File” menu, “New Project”, and click on it.

  2. The “New Project” will appear, select “Visual C#” then in the search box find the “Winodes Service(.NetFramework)” template, and select.

  3. Go to the name field and give a name to your project and click on the “OK” button.

  4. Then you will get the service1.cs in design mode, right-click on it and click on “View Code”.

  5. In code mode, you will find the complete class structure of service. Inside the service1 class by default, you will also find two methods (OnStart() and OnStop()).

  6. Apart from the above methods, the windows service also overrides more methods and the details of these methods are given below.

  7. Now go to the solution explorer, right-click on service(service1.cs) and click on properties.

  8. Here you can change your service filename, the service name will be the same as your service file name.

  9. Now you are done with your windows service creation. You can write logic inside the OnStart and OnStop methods. For example, if you want to execute code when service starts then you can write inside the OnStart method.

  10. Build your project by selecting Build Solution from the Build menu.

  11. Add the necessary installers for your service application.

  12. Now you are completely done with the windows service. Install the service. for more information on how to install windows service, see below visit the links given in the related article section.

Add Installer to Windows Service

Visual Studio has installation components that can install resources associated with service applications. Installation components register an individual service on the system to which it is being installed and let the Services Control Manager know that the service exists. When you work with a service application, you can select a link in the Properties window to automatically add the appropriate installers to your project.

When an installer is added to service project, a new class which is by default named ProjectInstaller is created in the project, and instances of the appropriate installation components are created within it. The ProjectInstaller class acts as a central point for all of the installation components the project needs. For example, if a second service is added to application and click the Add Installer link, a second installer class is not created; instead, the necessary additional installation component for the second service is added to the existing class.

There is no need to do any special coding within the installers to make services install correctly. However, there may occasionally need to modify the contents of the installers if you need to add special functionality to the installation process. Follow the below steps to add installer to Windows service.

  1. Access the Design view of the service for which you want to add an installation component
    • Go to Solution Explorer
    • Right on the service class.
    • Find and Click on the View Designer.
  2. ServiceName.cs[Design] file will open and appear on the file tab of Visual Studio, Right click on the designer and then click Add Installer.

  3. A new ProjectInstaller class, and two installation components, ServiceProcessInstaller and ServiceInstaller, are added to your project, and property values for the service are copied to the components.

  4. Verify that the value of the ServiceName property is set to the same value as the ServiceName property on the service itself in the ServiceInstaller component. Right click on the ServiceInstaller and click on the Property or you can select the component and press F4 button of key board.

  5. To change the behaviour of service or to determine how your service will be started, click the ServiceInstaller component and set the StartType property to the appropriate value.

  6. To determine the security context in which your service will run, click the ServiceProcessInstaller component and set the appropriate property values. For more information, see How to: Specify the Security Context for Services.

  7. Override any methods for which you need to perform custom processing.
  8. Perform steps 1 through 7 for each additional service in your project.

Windows Service Methods

The followings are the most common method using in windows service, apart from these there more methods which developers are using rarely.

  1. OnStart:- When implemented in a derived class, executes when a Start command is sent to the service by the Service Control Manager (SCM) or when the operating system starts (for a service that starts automatically). Specifies actions to take when the service starts.

  2. OnStop:- When implemented in a derived class, executes when a Stop command is sent to the service by the SCM. Specifies actions to take when a service stops running.

  3. OnPause:- When implemented in a derived class, executes when a Pause command is sent to the service by the SCM. Specifies actions to take when a service pauses.

  4. OnContinue:- When implemented in a derived class, OnContinue() runs when a Continue command is sent to the service by the SCM. Specifies actions to take when service resumes normal functioning after being paused.

  5. OnShutdown:- When implemented in a derived class, executes when the system is shutting down. Specifies what should occur immediately prior to the system shutting down.

Related Articles

  1. Install Uninstall Windows Service Using Batch File
  2. Install and Uninstall Windows Service

Summary

In the above of this blog post, we discussed what is Windows service and each steps in details to create a windows service in visual studio and the functions it can overrides from base class like what is the use of methods in windows service.

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال