Implementing the IFormatter Deserialize Method

The function of the IFormatter.Deserialize method is to deserialize an object graph from a stream and return the root object of the object graph. The following code implements the IFormatter.Deserialize method for the MyFormatter class object Deserialize(System.IO.Stream serializationStream) Create an object manager to help with deserialization. _om new ObjectManager( _surrogateselector, _streamingcontext ) _reader new StreamReader(serializationStream) Read objects until end of stream. Now we...

Contexts and Remoting

Recall that the context forms a .NET Remoting boundary around object instances within it. Figure 6-1 illustrates how the .NET Remoting infrastructure isolates an object instance within a context from object instances outside the context by using a special type of channel known as a cross-context channel and four chains of message sinks that separate inbound message processing from outbound message processing. Figure 6-1 Chains of message sinks isolate a ContextBoundObject instance from objects...

Message Sink

Any type that implements the IMessageSink interface can participate in the .NET Remoting architecture as a message sink. Table 6-1 lists the members of the IMessageSink interface. Table 6-1 Members of Table 6-1 Members of The next message sink in the chain, or null if this is the last sink in the chain Processes the message asynchronously The following code defines a class that implements IMessageSink public class PassThruMessageSink IMessageSink public PassThruMessageSink IMessageSink next...

Context Attributes and Properties

You define and establish a context by attributing the context-bound type with one or more attributes that implement the IContextAttribute interface. Table 6-2 shows the methods that IContextAttribute defines. Table 6-2 Members of Table 6-2 Members of The runtime calls this method to determine whether the current context is OK for activation of the attributed type. The runtime calls this method after an attribute has indicated that the current context isn't OK for activation of the attributed...

Implementing a Custom Channel Sink

Request and response messages pass from one end of a sink chain to the other. During this traversal, sinks have the opportunity to do work based on the message information. By creating a custom sink, we can hook ourselves into the sink chain and gain access to every message before transmission and after receipt. But custom sinks aren't limited to just manipulating messages. A custom sink could perform some action based on the contents of the message. Finally, custom sinks don't have to be...

Client Formatter Sink

The first sink in the client-side channel sink chain is an instance of a client formatter sink that implements the IClientFormatterSink interface. The client formatter sink acts as a bridge between the message sink chain and the channel sink chain. As such, the client formatter sink is both a message sink and a channel sink. The IClientFormatterSink interface is a composite of the IMessageSink, IClientChannelSink, and IChannelSinkBase interfaces. The following code listing defines a class named...

Finally the message contains information identifying the calling application domain context and process as well as

lt SOAP-ENC Array id ref-18 lt item href ref-42 gt lt item href ref-43 gt lt SOAP-ENC Array gt lt a3 CrossAppDomainData id ref-42 lt _DomainID gt 1 lt _DomainID gt lt _processGuid id ref-44 gt 20c23b9b_4d09_46a8_bc29_10037f04f46f lt _processGuid gt lt a3 CrossAppDomainData gt lt a3 ChannelDataStore id ref-43 lt _channelURIs href ref-45 gt lt _extraData xsi null 1 gt lt a3 ChannelDataStore gt lt SOAP-ENC Array id ref-45 lt item id ref-4 lt SOAP-ENV Body gt lt SOAP-ENV Envelope gt

Server FormatterSink Provider

Now that we have a server formatter sink, we need a channel sink provider class that we can use to install the formatter sink into the server channel sink chain. The following code listing defines the MyFormatterServerSinkProvider class public class MyFormatterServerSinkProvider public MyFormatterServerSinkProvider This ctor form provides properties from configuration file, public MyFormatterServerSinkProvider IDictionary properties, private IServerChannelSinkProvider _Next public...

Singleton

No more than one instance of a Singleton-mode-configured type will be active at any time. An instance is activated when first accessed by a client if no other instance exists. While active, the Singleton instance will handle all subsequent client access requests by either the same client or other clients. The Singleton instance can maintain state between method calls. The following code snippet shows the programmatic method of configuring a remotable object type as a Singleton in a server...

Heres the Proxy Attribute listing for our activation example

Public class SProxyAttribute ProxyAttribute public override System.MarshalByRefObject CreateInstance System.Type serverType Instance Get a proxy to the real object. MarshalByRefObject mbr base.CreateInstance serverType Are we on the client side or the server side This matters because the .NET Remoting infrastructure on both sides of the boundary will invoke this method. If we are on the server side, we need to return the MBR provided by the previous base.CreateInstance call, rather than our...

Extending Real Proxy

To write a proxy to replace RemotingProxy, we need to derive a class from RealProxy and add a new constructor and private MarshalByRefObject, as shown here public MyProxy Type type, MarshalByRefObject target We need to capture the reference to the real MarshalByRefObject so that our proxy can forward calls to the real remote object. We also call the RealProxy's constructor and pass the type of the remote object so that the .NET Remoting infrastructure can generate a TransparentProxy instance to...

Implementing the Formatter Interface

Serialization formatters are classes that implement the IFormatter interface. Table 8-8 lists the members defined by the IFormatter interface. Table 8-8 Table 8-8 Members of System.Runtime.Serialization. IFormatter Table 8-8 Table 8-8 Members of System.Runtime.Serialization. IFormatter Allows equipping the formatter instance with a SerializationBinder instance to deserialize a serialized type to a different type Can reference a StreamingContext instance that contains information about the...