How to Create Windows Service in Visual Studio 2017

Kailash Chandra Behera | Friday, October 23, 2020

Introduction

Here in this blog, we are going to discuss what is windows service and will discuss the step by step process, how to create a windows service in visual studio 2017. Here we will also discuss the functions or methods available in 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.

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.

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, we discussed each steps in details to create windows service in visual studio 2017 and the functions it can overrides from base class like what is the use of methods in windows service.

Thanks