Pluggable Windows Services

Source: Internet
Author: User

How to Implement pluggable Windows Services
There are many resources related to Windows Service creation, and there are also many such resources in the garden.Article.
I sorted out the related blog as follows,
Http://www.cnblogs.com/wuxilin/archive/2006/06/04/416838.html
Http://www.cnblogs.com/wujm/archive/2005/05/12/154369.html
Http://www.cnblogs.com/caca/archive/2005/02/25/109028.html
Http://www.cnblogs.com/laiwen/archive/2005/08/21/219590.html

The procedure is as follows:
1. Create a service:
1.1 we should create a Windows Service Project. Ide will automatically inherit a class from the servicebase class, which contains the on_star and on_stop methods, you can write the startup logic and stop logic in it.
In the design view, we can modify service attributes.
Whether autolog is automatically written to the System Log File
Receive Power Events During canhandlepowerevent Service
Whether the canpauseandcontinue Service accepts the pause or continue running request
Whether the canshutdown Service receives a notification when the computer that runs it shuts down, so that it can call the onshutdown Process
Whether the canstop Service accepts the stop operation request
Servicename service name
1.2 select a service component and switch to the design mode. Right-click and choose add installer to generate the installation file. For details about Attribute Modification in the installation file, refer to the following:
The projectinstall parameter modifies the account (specifies whether the user is a local system user)
Serviceinstall parameter modification:
Description: description,
Display name: displayname,
Serviname: Service name. It must be consistent with the service name just created.
Starttype :( Manual: after the service is installed, it must be manually started. Automatic: the service is automatically started every time the computer restarts; Disabled: the service cannot be started)

2 Installation
Install the service using the tool installutil provided by Microsoft. This tool is available in C:/Windows/wicrosoft.net/framework/?version=
Use installutil for installation and installutil/U for anti-Installation

If there is anything else, refer to the blog just mentioned. OK, everything is simple. However, if you want to implement an enterprise-level application, there will often be other scenarios. The following describes the scenario requirements I have met.
1. our Windows Service may satisfy more than one business logic. For example, if your machine is installed with Oracle, there will be an oracleservice, when we start the database, we can only start the TNS service and oracleservice, but in fact, when we start the oracleservice, several services will be started at the same time, such as dbwr (data file writing ), lgwr (Log File writing), SMON (system monitoring), pmon (user process monitoring), ckpt (checkpoint, synchronization data file, log file, control file, etc ). Sometimes, for the sake of performance, a service may need to run the business logic in multiple threads. For example, if the data volume of an imported file is large, consider this. Generally, you can run multiple tasks in two ways: 1. open multiple threads; 2. Sequentially execute tasks in one thread (time control can be added ). I think the most flexible method here is to use. Net's provincial multi-threaded support.
2. You must be able to quickly support new service requirements without changing the system structure. This indicates that the processing logic of the related classes cannot be written in the OnStar method, and it must be decoupled using some method.

Implementation Method:
Refer to the following class diagram

The servcive class is decoupled from the specific business implementation class. We can use the factory mode to generate the business logic class. Each business logic class is started in a new thread. net factory mode can be easily implemented using reflection. The Code is as follows:

Typeinfo = Node. attributes [ " Type " ]. Value;
Type type = Type. GetType (typeinfo );
Iservice instance = (Iservice) activator. createinstance (type );
// Initialize Service
Instance. initialize (node );
Instancearray. Add (instance );

// Run the service in the new thread. Each Service uses the same security context.
Threadstart TS =   New Threadstart (instance. Start );
Thread t =   New Thread (TS );
T. Start ();

Read the business logic class to be loaded from the configuration file, and call the start method with threadstart after instantiation to start a new thread. The login block of Enterprise libary can be used to read configuration files.
Call the stop method of each class in the onstop method. To call each stop method asynchronously, use delegate to encapsulate the stop method of the interface.
Ininvoke method implements asynchronous call

Foreach ( Object O In Instancearray)
{
Try
{
Iservice Service = (Iservice) O;
If (Service ! = Null )
{
//Call the stop method of each Service asynchronously to stop service components.
Onstopdelegate OSD= NewOnstopdelegate (service. Stop );
OSD. begininvoke (Null,Null);
}
}
Catch (Exception ex)
{
//
}
}

In this way, the entire Windows service is implemented. An additional advantage here is that it can be deployed in hot mode, that is, you do not need to delete the deployed Windows service, overwrite the new DLL, overwrite the previous DLL, and modify *. EXE. config File and restart it. In fact, I think it may take less time to start the service after re-Compiling and deploying the service.

About debugging:
A common debugging method for Windows Services is to use debugging-> attach to a process.
Select to display all user processes. The Windows service processes we have deployed and started will appear and jump to the breakpoint we have set. However, it is troublesome to debug the onstart part of the service. I recommend that you create a porjec for debugging. The onstart part can be written here.CodeThere are not many codes, which is very convenient. You can debug windowsProgramDebugging the Windows service code.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.