Developing a Windows service application under .NET.

xiaoxiao2021-03-06  19

The Windows service application describes the Microsoft Windows service (ie, the previous NT service) enables you to create executable applications that can be run for a long time in their own Windows sessions. These services can be automatically launched when the computer is started, and can be paused and restarted without displaying any user interface. This makes the service very suitable for use on the server, or at any time, in order not to affect other users working on the same computer, it takes a long run function for a long time. You can also run the service in a secure context that is different from a specific user account or the default computer account that is different from the registration user.

Creating a Windows Service When you create a service, you can use the Visual Studio .NET project template called a Windows service. This template references the appropriate class and namespace to set the inheritance from the service base class and rewrite several ways you may have to rewrite, so that most of your work is automatically completed.

To create a functional service, you must at least:

Set the serviceName property. Create a required installer for the service application. Override onStart and onStop methods and specify the code to customize the behavior of custom services.

After adding the installer to the application, the next step is to create a installation item, which will install the compiled project file and run the installer required to install the service. To create a complete installation item, you must add the output of the service item to the installation item, then add a custom action to install your service.

Create a Windows service item. In the Properties window, set the service serviceName properties. (Note that the value of the serviceName property must always match the name recorded in the installer class. If this property is changed, you must update the ServiceName property in the installer class.) Set all the following properties to determine the working mechanism of the service.

Property Settings CANSTOPTRUE Represents service will accept requests for stopping; false means that the service stops. CanshutDownTrue means that the service hopes to receive a notification when running its computer to be turned off to call the onshutdown process. CanpauseAndContinuetrue means that the service will receive a request for suspend or continue running; False means that the service is suspended and continues. CanhandLePowerEventTrue indicates that the service can handle notifications for computer power status changes; false represents a notification of prohibition services to get these changes. Autolog True means that the information item writes the information to the application when the service is executed; false represents this feature. (Note that the default is true.) Note When the Canstop or CanpauseAndContinue is set to false, the Service Control Manager will disable the corresponding menu option to stop, suspend, or continue the service. Access the code editor and fill in the onStart and the onstop process. Rewrite all other methods to define their definitions. Add the installer required to add a service application. Generate a project by selecting "Generate Solution" from the Generate menu. (Note Do not run the project by pressing the F5 key, and cannot run the service item in this way.)

Example: Create a new Windows service item called MyService, change the class name in Service1.cs and code to the name you want, I will use myService; use system.threading; a background service thread, private thread backthread = NULL; instantiated thread in the constructor, this.backthread = New Thread (New ThreadStart (THISRUNNING)); add private void running ()} method in the class, usually let the thread have been in operation to handle you Give it a job, such as based on messages in the message queue, perform download tasks download files. Private void Running () {while {// Your handle thread.sleep (5 * 1000); //, for example, let thread sleep 5 second}} Rewind thread control function protected override void onstart (String [] args ) {this.backThread.Start ();} protected override void OnStop () {this.backThread.Abort ();} protected override void OnPause () {this.backThread.Suspend ();} protected override void OnContinue () { THIS.BACKTHREAD.RESUME ();} At the same time, you can also set up the properties listed above as needed. On this project, right click Select Add - Add Class - Installer class, name INSTALL.CS Add a ServiceInstall and ServiceProcessInstall control for it in Install.cs View Designer, you can set properties for these 2 controls, for example Set service dependencies in the serviceIndependedon property of ServiceInstall. Generate a solution, hereby describe how to install service installation and uninstall services

Manually install service

Access the catalog of the compiled executable in the project. Use the output of the project as a parameter, running installutil.exe from the command line. Enter the following code in the command line: InstallUTIL YourProject.exe

Manual uninstall service

Use the output of the project as a parameter, running installutil.exe from the command line. Enter the following code in the command line: installutil / u YourProject.exe

Debug Windows service application

Since the service must run from the context of the service control manager, not from the Visual Studio .NET, the debugging service is not as simple as in debuging other Visual Studio application types. To debug the service, you must start the service first, then attach a debugger to the process being running service. You can then use all standard debugging features of Visual Studio to debug the application.

WARNING Do not attach the process unless you know what the process is, and you know the consequences of the process or may cancel the process. For example, if you appended to the Winlogon process, then stop debugging, the system will be suspended because there is no Winlogon, the system cannot run.

You can only attach the debugger to the running service. The additional process interrupts the current operation of the service; it does not really terminate or suspend the processing of the service. That is, if the service is running while starting debugging, the service is still in the "Started" state when debugging, but its processing has been hanged.

The process of attaching to the service allows you to debug most service code, but not all; for example, because the service has been launched, can not use this method to debug the code in the onstart method of the service, or debug the main method for loading services Code. One way to resolve this is to create a temporary service in the unique role is to help debug a service application. You can install both services and start this "virtual" service load service process. Once the temporary service launches the process, you can use the Debug menu in Visual Studio .NET to attach to the service process. When attached to this process, you can set a breakpoint and use these breakpoints to debug code. When exiting the dialog box for attaching to the process, it is actually in debug mode. You can start, stop, suspend, and continue your service using the Service Control Manager, so the breakpoints already set. After the debugging is successful, remove this "virtual" service.

Note that the debug onStart method may be more difficult because the Windows Service Manager limits all the time to start the service within 30 seconds. The service is interacting with the Windows Service Manager when debugging the Windows service application. "Service Manager" by call

OnStart method launches service, then spend 30 seconds waiting

The onStart method returns. If the method is not returned during this time, the manager will display an error that the service cannot start. If

A breakpoint is placed in the onStart method and does not pass the breakpoint within 30 seconds, and the manager does not start the service.

Debugging service

Install your service. You can start the service from the Service Control Manager, Server Explorer, or code. In Visual Studio, select Process from the Debug menu. The Process dialog box appears. Click Show System Process. Click the service process in the "Available Process" area, and then click Add. (Tip This process will be the same name with the executable of the service.) Addition to Process dialog box. Select any appropriate option and click OK to close the dialog. (Note You are now in debug mode.) Settings to have any breakpoints you want to use in your code. Access the Service Control Manager and manipulate your service and send it, suspend, and continuing commands to hit your breakpoint.

I am not very thorough about Windows Service, welcome to discuss and learn together.

转载请注明原文地址:https://www.9cbs.com/read-53577.html

New Post(0)