Windows services are applications that can be opened automatically when the operating system starts. Windows services can run without an interactive user logging on to the system. You can configure the Windows service to run from a specially configured user account or a system user account. Take VS2008, for example, to see the simple use of. NET Windows services.
First, create a Windows service
Create a new project, Visual c#-> windows–> Windows Service, change the project name to Winservicetest, modify the corresponding location of the project, as shown in 1,
Figure 1
When you click OK, you will see a designer that looks similar to the Windows Forms application, but you cannot insert the Windows Forms component in it, because the WINDWS service cannot display any information directly on the screen, as shown in Figure 2,
Figure 2
Open the Properties editing window of the service to configure the properties of the service, as shown in Figure 3,
Figure 3
Use the service properties to set the following values:
- AUTOLOG Specifies that events that start the service and stop the service are automatically written to the log file.
- CanPauseAndContinue, CanShutdown, and CanStop specify that the service can handle specific requests for pausing, continuing, shutting down, and stopping services.
- ServiceName is the name of the service written to the registry, which can be used to control the service.
- The canhandlepowerevent option is valid for services running on a laptop computer. If this option is enabled, the service can respond to low power events to change how the service operates.
Changes to any property are reflected in the. The InitializeComponent method in the Designer.cs file is similar to Windows Forms. The main function of the application in the Program.cs file generated when the project was created, in the main function, declares an array of elements of the ServiceBase class ServicesToRun, if the service process to run multiple services, you need to add multiple instances of the specific service class into the array, and then the Servic The Estorun array is passed to the static method of the ServiceBase class Run (); If there is only one service in the process, you can delete the array, and the Run () method receives a single object derived from ServiceBase.
The OnStart () method is called when the service starts, and you can add handlers in the OnStart () method, such as setting the clock, starting the thread, and so on. The OnStop () method is called when the service stops, and you can add handlers in the OnStop () method, such as turning off the clock, stopping the thread, and so on. If you set the related properties of the service, you can also override the OnPause (), OnContinue (), OnShutdown (), Oncustomcommand () methods in the service class, where Oncustomcommand () Handlers can serve custom commands that the service control program sends over.
II. Installation of Windows services
To install the service you must add the Installer class for the service. Switch to the Design view of Visual Studio, right-click in the space and select Add Installer, the Component Designer will create a new Projectinstall class, An ServiceInstall instance and an ServiceProcessInstaller instance. The ServiceProcessInstaller class is used to configure the process to define attribute values for all services in the process (remember that a service process can contain multiple services), which are described in the following table:
Property |
Describe |
| Username,password |
The Accout property is set to Serviceaccout.user, the Username property and the password attribute indicate which account the service is running under |
| Account |
Using this property, you can specify the account type of the service |
| HelpText |
The HelpText property is a read-only property, and the style it returns is used to help set the user name and password |
Table 1
The Account property value of the ServiceProcessInstaller class can be any value of the Serviceacount enumeration, as shown in table 2
| < P align= "Center" value |
|
| Setting this value specifies that the service uses a user account with high privileges on the local system, but this account allows anonymous users to enter the network, so it does not have permissions on the network |
| Local Service |
This account type provides computer certificates to any remote server |
| similar to LocalService, This value specifies that the computer's certificate is routed to the remote server, but unlike LocalSystem, the service can log on to the local system as a non-authorized user . As the name implies, this account can only be used for services that need access to resources from the network |
| User |
Set the Account property to Serviceaccount.user, which means that you can specify the accounts that should be used in the service |
Table 2
ServiceInstaller the classes that each service needs, the properties of this class can be used for the first service in the process, with properties such as StartType, DisplayName, ServiceName, and Servicesdependedon, as shown in table 3.
Property |
Description |
| StartType |
StartType Indicates whether the service starts manually or automatically. Its value can be: serivestartmode.automatic, Servicestartmode.manual, servicestartmode.disabled. If you use serviecstartmode.disabled, the service cannot be started. This option is available for services that should not be started in the system. For example, if you don't get the hardware controller you need, you can set this option to disable. |
| DisplayName |
DisplayName is the friendly name of the service that is displayed to the user. In addition, this name is also used to control and monitor service management tools. |
| ServiceName |
ServiceName is the name of the service. This value must be consistent with the ServiceName property of the ServiceBase class in the service program, which is related to the service program that configures ServiceInstaller as needed. |
| Servicedespendedon |
Specifies a service group that must be started before the service starts. When the service starts, all dependent services are started automatically, and our services are started. |
After you add the installation class to your project, you can use the Installutil.exe utility to install and uninstall the service, which can be used to install all assemblies that contain the install class. The commands for installing and uninstalling the service are as follows:
InstallUtil WinServiceTest.exe
Installutil/u WinServiceTest.exe
You can also use the Windows-brought service installation commands, such as
SC start WinServiceTest.exe
SC Delete WinServiceTest.exe
Third, the Windows Service installation package production
Although you can use the InstallUtil and SC commands to install the service, for more intuitive and convenient, you can package the service as an installation package and then install it via the interface prompts. The steps are as follows:
1. Adding installation and Deployment projects
Figure 4
2. Add WinServiceTest.exe to the installation project
In Solution Explorer, right-click MyServiceSetup, point to Add, and then select Project output, and the Add Project Output Group dialog box appears. In the Project box, select Winservicetest, in the list box, select Primary Output, and then click OK, and the project item for the Winservicetest main output is added to the Setup project, as shown in Figure 5.
Figure 5
3. Adding a custom action to a setup project
The Custom Actions Editor appears in Solution Explorer by right-clicking the Setup project, pointing to view, and then selecting Custom action.
Figure 6
In the Custom Actions Editor, right-click the Custom Actions node and select Add Custom Action, and the Select Items in Project dialog box appears.
Figure 7
In the list box, double-click Application folders to open it, select Primary output from Winservicetest (active), and then click OK.
Figure 8
The primary output is added to all four nodes of the custom action, namely "Install", "commit", "rollback", and "unload".
Figure 9
4. Build the Installation Project
In Solution Explorer, right-click the Winservicetest project and choose Build. To install the service, right-click the Setup project in Solution Explorer and select Install to complete the installation as prompted by the interface.
C # Windows Services