c detailed steps for creating windows service of Windows Services

  • 2020-05-26 10:01:53
  • OfStack

The Windows service was called the NT service in the previous version of Visual Studio, with the new name enabled in VS.net. Creating an Windows service with Visual C# is not a difficult task. This article will walk you through the steps of creating an Windows service and using it. The service writes some text messages to a text file when it starts and stops.
Step 1: create the service framework

To create a new Windows service, select the Windows service (Windows Service) option from the Visual C# project, give the project a new file name, and click ok.
As you can see, add WebService1 to the pilot project file.cs class:
The meaning of each attribute is:
Does Autolog write to the system's log files automatically
The CanHandlePowerEvent service accepts power events
Whether the CanPauseAndContinue service accepts requests to suspend or continue running
Whether the CanShutdown service is notified when the machine running it is shut down so that it can invoke the OnShutDown procedure
Whether the CanStop service accepts requests to stop running
ServiceName service name

Step 2: add functionality to the service

As you can see in the.cs code file, there are two ignored functions, OnStart and OnStop.
The OnStart function is executed when the service is started and the OnStop function is executed when the service is stopped. Here, when the service is started and stopped, write some text messages to a text file. The code is as follows:


protected override void OnStart(string[] args)
{
 FileStream fs = new FileStream(@"d:\mcWindowsService.txt" , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine("mcWindowsService:         Service Started"+DateTime.Now.ToString()+"\n");
m_streamWriter.Flush();
m_streamWriter.Close();
fs.Close();

}

protected override void OnStop()
{
FileStream fs = new FileStream(@"d:\mcWindowsService.txt" , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(" mcWindowsService: Service Stopped "+DateTime.Now.ToString()+"\n");
m_streamWriter.Flush();
m_streamWriter.Close();
fs.Close();
}

Step 3: add the installer to the service application

Visual Studio.NET comes with an installation component that you can use to install the resources associated with the service application. The installation component registers a single service on the system to which it is being installed and makes the service control manager aware of its existence.
To install the service correctly, no special coding is required in the installer. However, if you need to add special features to the installation process, you may occasionally need to modify the contents of the installation program.

The steps to add the installer to the service application are:

1: in the solution, access the Design view of the service to which you want to add the installation component.

2: in the properties window, click add installer link
At this point, a new class ProjectInstaller and two installation components ServiceProcessInstaller and ServiceInstaller are added to the project, and the property values of the service are copied to the component.

3: to determine how to start the service, click the ServiceInstaller component and set the StartType property to the appropriate value.
After the Manual service is installed, it must be started manually.
The service starts automatically every time the Automatic computer is restarted.
The Disabled service could not start.

4: change the Account attribute of serviceProcessInstaller class to LocalSystem
This way, the service will always start regardless of which user is logged into the system.

Step 4: generate the service program

Build the project by selecting build from the build menu.
Be careful not to run the project by pressing F5 - you cannot run the service project this way.
Step 5: install the service
Access the directory where the compiled executable files in the project reside.
With the output of the item as a parameter, run InstallUtil.exe from the command line. Enter the following code on the command line:


installutil yourproject.exe

Unloading service

Run InstallUtil.exe from the command line with the output of the item as a parameter.


installutil /u yourproject.exe


Related articles: