I. remoting technology introduction:
. Net remoting is a framework for implementing distributed object systems on the. NET platform. It can be used to access another application.ProgramObjects in a domain (or another server) can be inter-process or inter-process of different systems. This CS-type communication mechanism is more convenient and fast. The essence is: the client creates a proxy for the server object and calls its method to transmit it to the server through a channel network, process the parameter data passed by the client on the server and return the receiving variable (if any) to the client. This method is significantly different from traditional method calls, And it easily implements distributed data processing.
Understanding the basic principles of the remoting framework
. Net remoting Architecture
. Net remoting implements object communication between two application domains through channels. There are two main remoting channels: TCP and HTTP. In. net, the ichannel interface is defined in system. runtime. remoting. Channel. The ichannel interface includes the tcpchannel and HTTP channel types. They correspond to the two types of remoting channels respectively. The channel object represents the connection to a remote application. Each channel object also contains a formatting program object that converts a method call to a message in a known format.
2. Two remoting application implementation methods:
Remoting applications are generally divided into three parts: client, server, and remote. The client is the caller and the superior of the command. The server is the party that processes the data, which is equivalent to the subordinate who receives the command. The remote class is the transmitted data and is related to the Command Carrier. A complete remoting application must include these three parts. Generally, there are two ways to implement remoting applications: WritingCodeThe other method is to use the configuration file, but either method is required to write a remote class.
1. The remote type classes used in both methods:
Generally, we need to write a DLL that contains the remote class required by both the client and the server. This class must be inherited from system. marshalbyrefobject. The following is a simple example:
NamespaceRemoteobject {Public classRemoteobjclass: System.Marshalbyrefobject{Public intMulti (IntA,IntB ){ReturnA * B ;}} we save this class in the remoteobject project named assembly. After compilation, the remoteobject. dll will be generated for the subsequent client and server calls.
Because the remote class must be correctly referenced on both the client and server.
2. Server:
The server provides services. It requires the server process to register the remote class above, so that other applications (that is, clients) can activate and call it correctly.
1), the way to write code:
Based on how the object is activated, the server registers the object using two static methods:
Registeractivatedservicetype: registers the type of the object on the server as the type that can be activated from the client based on the request.
Or registerwellknownservicetype: register an object type as a known object type on the server.
The following statements are registered using registerwellknownservicetype.RemoteobjclassFor remote activation:
TcpchannelChnl =NewTcpchannel(8888); // The Listener port number enabled by the server for the current remote callChannelservices. Registerchannel (chnl,True);Remotingconfiguration. Registerwellknownservicetype (
Type. GetType ("Remoteobject. remoteobjclass, remoteobject"), // All remote classes in the assembly path and name
"Sampleremote", // Path for remote call
Wellknownobjectmode. Singleton );
2), using the configuration file:
First, you need to configure the configuration file as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<System. runtime. remoting> <! --.Net remoting configuration root node -->
<Application> <! -- Contains information about Remote Application Usage and common objects -->
<Service> <! -- Server, used to specify the way in which objects are exposed -->
<Wellknown mode = "Singleton" type = "remoteobject. remoteobjclass, remoteobject" objecturi = "sampleremote"/>
<! -- Activated is relative to wellknown. wellknown indicates the server-side activation object, and the latter indicates the client-side activation object. -->
<! -- Mode indicates how to respond to client requests, while Singleton indicates a single instance. -->
<! -- Type indicates the type to be published. Select the specified type name (including the namespace) and specify the. dll file to which the type belongs. -->
<! -- Objecturi indicates the remote object access path -->
</Service>
<Channels> <! -- Used to specify channel information. Multiple channels can be specified at the same time. -->
<Channel port = "8888" ref = "TCP"/> <! -- Specific channel information, port is the port number, and ref is the referenced channel type -->
<! --.Net framework provides HTTP and TCP channels -->
</Channels>
</Application>
</System. runtime. remoting>
</Configuration>
Next, we need to write the following code in the Code:
Remotingconfiguration. Configure (appdomain. currentdomain. setupinformation. configurationfile, false );
Both of the preceding methods can activate the server. When a client program calls a remote object, it automatically performs processing on the server.
3. Client:
1), the way to write code:
TcpclientchannelChnl =NewTcpclientchannel();Channelservices. Registerchannel (chnl,True); // Register the communication channelRemoteobjclass remonte OBJ= // Obtain the remote object proxy
(Remoteobjclass)Activator. GetObject (
Type. GetType ("Remoteobject. remoteobjclass, remoteobject"), // All remote classes in the assembly path and name
"TCP: // localhost: 8888/sampleremote"); // The URI used for calling
Console. Writeline (Remonte OBJ. Multi (); // call methods in the remote class
2), using the configuration file:
The configuration file is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<System. runtime. remoting>
<Application>
<Client>
<Wellknown url = "http: // 127.0.0.1: 8888/sampleremote" type = "remoteobject. remoteobjclass, remoteobject"/>
</Client>
<! -- Remote object access path. Both the domain name and IP address can be -->
<Channels>
<Channel port = "0" ref = "TCP"/>
<! -- If the port is 0, the client does not listen on any port. -->
</Channels>
</Application>
</System. runtime. remoting>
</Configuration>
Next, we need to write the following code in the Code:
Remotingconfiguration. Configure (appdomain. currentdomain. setupinformation. configurationfile, false );
Remoteobjclass remonte OBJ= NewRemoteobjclass();
Console. writeline (Remonte OBJ. Multi (3, 6 ));
Iii. Summary:
. Net remoting distributed development is more suitable for communications between local applications, especially windows and windows, web and windwos. It fully reflects the efficiency of. Net remoting. This article introduces the basic principle and simple application of remoting technology, and focuses on the two implementation methods of remoting Technology. Note that Channel services should be called in a timely manner when channels are no longer used. unregisterchannel () method. Xiao Ke has just been familiar with remoting technology. If it is inappropriate, please criticize and correct it and hope to provide reference value to readers and friends. More related knowledge, can refer to: http://www.cnblogs.com/rickie/category/5082.html
Conclusion