Document directory
- 1. Registration Channel
- 2. Register a remote object
- 3. Client Processing
Communication Channel establishment process 1. Registration Channel
Remoting technology supports two types of communication: server activation and client activation. This design uses the server activation mode.
When establishing a channel, you must process it on the server and client respectively.
The server process is as follows:
To communicate across application domains, you must implement a channel. As mentioned above, remoting provides the ichannel interface, which contains two types of channels: tcpchannel and httpchannel. In addition to the performance and the format of serialized data, these two types are implemented in the same way. Therefore, we will take tcpchannel as an example.
To register a tcpchannel, first add the reference "system. runtime. remoting" in the project, and then using namespace: system. runtime. remoting. Channel. TCP.
The Code is as follows:
Static void main (string [] ARGs) {# launch tcpserverchannel chancar = new tcpserverchannel (9999) on the server of region data table 1 (CAR); channelservices. registerchannel (chancar, false); console. writeline (chancar. channelname); remotingconfiguration. registerwellknownservicetype (typeof (carremoteserviceimpl), "service", wellknownobjectmode. singleton); console. writeline ("data table 1 (CAR) server startup... "); # endregion console. writeline ("press <enter> to exit... "); console. readline ();}
When instantiating a channel object, pass the port number as a parameter. Then call the static method registerchannel () to register the channel object.
2. Register a remote object
After the channel is registered, to activate a remote object, you must register the object in the channel. The method for registering objects varies depending on the activation mode.
(1) Singleton Mode
For wellknown objects, you can use the static remotingconfiguration. registerwellknownservicetype () method to implement:
Remotingconfiguration. registerwellknownservicetype
(Typeof (serverremoteobject. serverobject), "servicemessage", wellknownobjectmode. Singleton );
(2) singlecall Mode
The method for registering an object is basically the same as the singleton mode. You only need to change the enumeration parameter wellknownobjectmode to singlecall.
Remotingconfiguration. registerwellknownservicetype
(Typeof (serverremoteobject. serverobject), "servicemessage", wellknownobjectmode. singlecall );
3. Client Processing
The client mainly performs two tasks: first, registering a channel. In remoting, both the server and client must transmit messages through channels to obtain remote objects. The second step is to obtain the remote object.
Registration channel:
// Initialize the global variable tcpclientchannel Chan; // The Sub-server declares the ihotelremoteservice named remoteservice; icarremoteservice carremoteservice; iflightremoteservice flightremoteservice; icustomerremoteservice merremoteservice; ireservationremoteservice reservationremoteservice;
Chan = new tcpclientchannel (); channelservices. registerchannel (Chan, false); # region instantiates the remote object javasremoteservice = (imo-remoteservice) activator. getObject (typeof (iswitchremoteservice), "TCP: // localhost: 9998/service", null); carremoteservice = (icarremoteservice) activator. getObject (typeof (icarremoteservice), "TCP: // localhost: 9999/service", null); customerremoteservice = (icustomerremoteservice) activator. getObject (typeof (icustomerremoteservice), "TCP: // localhost: 9997/service", null); flightremoteservice = (iflightremoteservice) activator. getObject (typeof (iflightremoteservice), "TCP: // localhost: 9996/service", null); reservationremoteservice = (ireservationremoteservice) activator. getObject (typeof (ireservationremoteservice), "TCP: // localhost: 9995/service", null); # endregion
Note that when the client instantiates a channel, it is the default constructor called, that is, no port number is passed. In fact, this port number is indispensable, but its designation is put behind as part of the URI.
Obtain the remote object.
Similar to the server, different activation modes determine the implementation of the client. However, the difference is only the difference between the wellknown activation mode and the client activation mode. For singleton and singlecall modes, the client implementation is identical.
Wellknown activation mode
To obtain a well-known remote object on the server, you can obtain it using the GetObject () method of the activator process:
Serverremoteobject. serverobject serverobj = (serverremoteobject. serverobject)
Activator. GetObject (typeof
(Serverremoteobject. serverobject), "TCP: // localhost: 8080/servicemessage ");
First, it is activated in wellknown mode. The client obtains the object by using GetObject (). The first parameter is the remote object type. The second parameter is the server Uri. If it is an HTTP channel, it will naturally be used. Because I use a local machine, it is localhost. you can replace it with a specific server IP address. The port must be the same as the server port. The following is the remote object service name defined by the server, that is, the content of the applicationname attribute.