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 StartedMicrosoft 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
Open Microsoft visual studio, go to the “File” menu, “New Project”, and click on it.
The “New Project” will appear, select “Visual C#” then in the search box find the “Winodes Service(.NetFramework)” template, and select.
Go to the name field and give a name to your project and click on the “OK” button.
Then you will get the service1.cs in design mode, right-click on it and click on “View Code”.
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()).
Apart from the above methods, the windows service also overrides more methods and the details of these methods are given below.
Now go to the solution explorer, right-click on service(service1.cs) and click on properties.
Here you can change your service filename, the service name will be the same as your service file name.
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.
Build your project by selecting Build Solution from the Build menu.
Add the necessary installers for your service application.
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.
-
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.
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.
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.
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.
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.
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.
- Override any methods for which you need to perform custom processing.
- 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.
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.
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.
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.
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.
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.
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