Review. Net remoting (4)

Source: Internet
Author: User

(1) Framework of remoting

The remoting client communicates with the server on msdn. Communication between the client and the server is achieved by sending messages. Message Processing is performed by a series of communication channels created by the client and server. The client uses a proxy to convert a message call to an iMessage. The iMessage is passed into the communication processing chain, and the message is converted to a message stream through the client formattersink. Then, the message is further processed through the iclientchannelsink and then transmitted to the server. After receiving the message, the server exchanges the message with formattersink for remote object processing. The message processing process is collectively referred to as the channel receiver (such as formatting ).Program, Transport, or stack generator receiver), but you can customize the channel receiver chain to execute special tasks using messages or streams.Each channel receiver is either implementedIclientchannelsink, Or implementIserverchannelsink. The first channel receiver on the client must also be implementedImessagesink. It usually implementsIclientformattersink(Inherited from imessagesink, ichannelsinkbase, and iclientchannelsink) is called a formatter receiver because it converts incoming messages to streams (iMessage objects ).

The Channel Receiver provider (the object implementing iclientchannelsinkprovider, iclientformattersinkprovider, or iserverchannelsinkprovider) is responsible for creating a channel receiver that processes. NET remote messages. When activating the remote type, the Channel Receiver provider will be retrieved from the channel and called on the receiver providerCreatesinkMethod to retrieve the first channel receiver in the chain.

The channel receiver is responsible for transmitting messages between the client and the server. They connect to each other to form a chain. When called on the receiver providerCreatesinkMethod, the method should perform the following operations:

· Create a channel receiver.

· Called on the next receiver provider in the chainCreatesink.

· Ensure that the next receiver is linked with the current receiver.

· Return the receiver to the caller.

The channel receiver is responsible for forwarding all calls executed on these receivers to the next receiver in the chain, and should also provide a mechanism to store references to the next receiver.

Channel Receivers have great flexibility in what content is sent along the receiving chain. For example, a security receiver that negotiates identity authentication before sending the actual serialized original message can intercept the complete channel message and replace the content stream with its own content, then, the content stream is sent along the receiver chain to the remote application domain. During the return process, the security receiver can intercept the reply message and talk to the security receiver corresponding to the remote application domain. Once the Protocol is reached, the initial security receiver can send the original content stream to the remote application domain.

Message Processing in the channel receiver chain

Once the. NET remote processing system finds a channel that can process messages, the channel passes messages to the formatter channel receiver. This process is achieved by calling syncprocessmessage (or asyncprocessmessage. The formatter receiver creates a Transport Header array and calls getrequeststream for the next receiver. This call is forwarded down along the receiver chain, and any receiver can create a request stream that will be passed back to the formatter receiver. IfGetrequeststreamReturnNullReference (When Visual Basic isNothing), The formatter receiver will create its own receiver for serialization. Once this call is returned, the message is serialized and the corresponding message processing method is called on the first channel receiver of the receiver chain.

Although the receiver cannot write data to a stream, it can read data from the stream or pass a new stream along the desired position. The receiver can also add a header to the header array (if they were not previously called on the next receiverGetrequeststreamAnd add the calls to the receiver stack before forwarding them to the next receiver. (The function of synchronous stack is to allow asynchronous calls to callback the caller upon completion .) When a call arrives at the end of a chain, the transport receiver sends the header and serialized message to the server through a channel. On this server, the whole process is reversed. The transport receiver (on the server) retrieves headers and serializes messages from the server-side stream, and forwards them through the receiver chain until it reaches the formatter receiver. The formatter receiver deserializes the message and forwards it to the. NET remote processing system. The message is restored to method call and called on the server object.

Create a channel receiver chain

To create a new channel chain, you must implement and configure the. NET remote processing system to identifyIserverchannelsinkproviderOrIclientchannelsinkproviderYou can use these two implementations to create customIclientchannelsinkOrIserverchannelsinkImplementation, or the next receiver in the search chain. You can use the basechannelsinkwithproperties abstract class to help implement custom Channel Receivers.

Generate a Channel Receiver provider

When constructing a channel, an application can provide the server or client Channel Receiver provider as a parameter. The Channel Receiver provider should be stored in a chain. Before passing an external Channel Receiver provider to the channel constructor, the developer is responsible for linking all the Channel Receiver providers together. Therefore, the Channel Receiver provider implementsNextAttribute. The followingCodeThe example illustrates how to generate a client-side channel receiver provider. The complete example can be found in the remote processing example: Channel Receiver provider.

Formatting program Receiver

The formatter receiver uses channel messages as implementation IMessage And serialized as a message stream.Some formatting program Receivers use the formatting program types provided by the system (binaryformatter and soapformatter ). Other implementations can use their own methods to convert channel messages to streams.

The formatter receiver is used to generate the required header and serialize the message into a stream. After formatting the program receiver, the message will pass Syncprocessmessage Or Asyncprocessmessage The call is forwarded to all receivers in the receiver chain. In this phase, the message has been serialized and cannot be modified.

 

You must create or modify the receiver of the message before the formatting program in the receiver chain. ImplementationIclientformattersinkThis can be easily implemented to tell the system that it already has reference to the formatter receiver. Then, you can place the real formatting program receiver at the end of the receiver chain.

During the return process, The formatter receiver switches the message flow back to the Channel message object (return message ). The first receiver on the client must be implementedIclientformattersinkInterface. When createsink returns a channel, the returned reference is forcibly convertedIclientformattersinkType, so that you can callSyncprocessmessageMethod. Remember,IclientformattersinkYesfromImessagesinkDerived from. If the forced conversion fails, an exception is thrown.

Custom Channel Receiver

On the client, the custom channel receiver is inserted into the object chain between the formatter receiver and the last transport receiver.By inserting a custom channel receiver in a client or server channel, you can process the receiver at one of the following time points:IMessage:

· In the process that a call that is represented as a message is converted to a stream and sent over the network.

· The process in which the stream is taken out from the network and sent to the last message receiver before the remote object on the server or proxy object (on the client.

The custom receiver can read data from the stream or write data to the stream (depending on whether it is an outgoing call or an incoming call), and can add additional information to the header as needed. In this phase, the message has been serialized by the formatting program and cannot be modified.When a message call is forwarded to a transport receiver at the end of the chain, the transport receiver writes the header to the stream and sends the transfer to the transport receiver on the server using a channel-specific transport protocol.

Transmission Receiver

The transmission receiver is the last receiver in the client chain and the first receiver in the server chain. In addition to transmitting serialized messages, the transport receiver is also responsible for sending headers to the server and retrieving headers and streams when the server returns a call.These receivers are built into the channel and cannot be expanded.

(2) transparentproxy and realproxy

The proxy mentioned above has to mention transparentproxy (transparent proxy) and realproxy (real proxy). Proxy is a bridge between local objects and remote objects. by inheriting realproxy, convert a method call to an invoke-based (iMessage MSG) format.

// Create a custom 'realproxy '. public class myproxy: realproxy {string myuristring; financialbyrefobject myexternalbyrefobject; [permissionset (securityaction. linkdemand)] public myproxy (type mytype): Base (mytype) {// realproxy uses the type to generate a transparent proxy. myexternalbyrefobject = (externalbyrefobject) activator. createinstance (mytype); // get 'objref ', for transmission serialization Between application domains. objref myobjref = remotingservices. marshal (myexternalbyrefobject); // get the 'uri 'Property of 'objref 'and store it. myuristring = myobjref. uri; console. writeline ("URI: {0}", myobjref. uri);} [securitypermissionattribute (securityaction. linkdemand, flags = securitypermissionflag. infrastructure)] public override iMessage invoke (iMessage myimessage) {console. writeline ("My Proxy. invoke start "); console. writeline (""); If (myimessage is imethodcallmessage) console. writeline ("imethodcallmessage"); If (myimessage is imethodreturnmessage) console. writeline ("imethodreturnmessage"); Type msgtype = myimessage. getType (); console. writeline ("Message Type: {0}", msgtype. tostring (); console. writeline ("message properties"); idictionary myidictionary = myimessage. properties; // s Et the '_ URI 'Property of 'imessage' to 'uri 'Property of 'objref '. myidictionary ["_ Uri"] = myuristring; idictionaryenumerator myidictionaryenumerator = (idictionaryenumerator) myidictionary. getenumerator (); While (myidictionaryenumerator. movenext () {object mykey = myidictionaryenumerator. key; string mykeyname = mykey. tostring (); object myvalue = myidictionaryenumerator. value; console. writelin E ("\ t {0 }:{ 1}", mykeyname, myidictionaryenumerator. value); If (mykeyname = "_ ARGs") {object [] myobjectarray = (object []) myvalue; For (INT aindex = 0; aindex <myobjectarray. length; aindex ++) console. writeline ("\ t \ targ: {0} myvalue: {1}", aindex, myobjectarray [aindex]);} If (mykeyname = "_ methodsignature ") & (null! = Myvalue) {object [] myobjectarray = (object []) myvalue; For (INT aindex = 0; aindex <myobjectarray. length; aindex ++) console. writeline ("\ t \ targ: {0} myvalue: {1}", aindex, myobjectarray [aindex]) ;}} iMessage myreturnmessage; myidictionary ["_ Uri"] = myuristring; console. writeline ("_ URI {0}", myidictionary ["_ Uri"]); console. writeline ("channelservices. syncdispatchmessage "); myreturnmessage = channelservices. syncdispatchmessage (myimessage); // push return value and out parameters back onto stack. imethodreturnmessage mymethodreturnmessage = (imethodreturnmessage) myreturnmessage; console. writeline ("imethodreturnmessage. returnvalue: {0} ", mymethodreturnmessage. returnvalue); console. writeline ("myproxy. invoke-finish "); Return myreturnmessage ;}}

(3) iMessage

(4) iclientformattersinkprovider and iclientformattersink, imessagesink

Both binaryclientformattersinkprovider and soapclientformattersinkprovider implement the iclientchannelsinkprovider interface, which is used to generate the iclientchannelsink in the client channel.

The instance objects binaryclientformattersink and soapclientformattersink are used to process messages and request data streams. Let's take a look at the methods of iclientchannelsinkprovider and iclientchannelsink.

Public interface iclientchannelsinkprovider

{

Iclientchannelsinkprovider next {Get; set ;}

Iclientchannelsink createsink (ichannelsender channel, string URL, object remotechanneldata );

}

Iclientchannelsink Method

Public interface iclientchannelsink: ichannelsinkbase

{

Iclientchannelsink nextchannelsink {Get ;}

Void asyncprocessrequest (iclientchannelsinkstack sinkstack, iMessage MSG, itransportheaders headers, stream );

Void asyncprocessresponse (iclientresponsechannelsinkstack sinkstack, object state, itransportheaders headers, stream );

Stream getrequeststream (iMessage MSG, itransportheaders headers );

Void processmessage (iMessage MSG, itransportheaders requestheaders, stream requeststream, out itransportheaders responseheaders, out stream responsestream );

}

Therefore, if you need to customize your own message processing, the client can implement the iclientchannelsinkprovider and iclientchannelsink interfaces, and then register them in the message processing chain through registration or configuration files. The first receiver on the client must be implementedIclientformattersinkThe message is serialized as a message stream.

(5) iserverformattersinkprovider and iserverchannelsink

On the server side, the binaryserverformattersinkprovider and soapserverformattersinkprovider of iseverchannelsinkprovider are implemented, and the binaryseverformattersink and soapserverformattersink for message processing are generated to process the messages sent from the client, this interface is required by the server to process messages, while the iseverchannelsinkprovider is required by the message processing object provider.

Iserverchannelsinkprovider Method

Public interface iserverchannelsinkprovider

{

Iserverchannelsinkprovider next {Get; set ;}

Iserverchannelsink createsink (ichannelpolicer channel );

Void getchanneldata (ichanneldatastore channeldata );

}

Iserverchannelsink Method

Public interface iserverchannelsink: ichannelsinkbase

{

Iserverchannelsink nextchannelsink {Get ;}

Void asyncprocessresponse (iserverresponsechannelsinkstack sinkstack, object state, iMessage MSG, itransportheaders headers, stream );

Stream getresponsestream (iserverresponsechannelsinkstack sinkstack, object state, iMessage MSG, itransportheaders headers );

Serverprocessing processmessage (receivsinkstack, iMessage requestmsg, itransportheaders requestheaders, stream requeststream, out iMessage responsemsg, out itransportheaders responseheaders, out stream responsestream );

}

If the server extends message processing, these two interfaces must be implemented.

(6) ichannel

Messages can be transmitted through httpchannel, tcpchannel, ipcchannel. For information on channel selection, see ms-help: // Ms. msdnqtr. v90.chs/ws_fxremoting/html/c4959f26-a935-42fd-8dcf-0c35de5fb860.htm.

(7) Scaling

To create a new channel chain, you must implement and configure the. NET remote processing system to identifyIserverchannelsinkproviderOrIclientchannelsinkproviderYou can use these two implementations to create customIclientchannelsinkOrIserverchannelsinkImplementation, or the next receiver in the search chain. You can use the basechannelsinkwithproperties abstract class to help implement custom Channel Receivers.

(8) AOP

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.