Remotable Types

Depending on its category, a remotable type can pass through .NET Remoting boundaries or be accessed over .NET Remoting boundaries. .NET Remoting defines three categories of remotable types: marshal-by-value, marshal-by-reference, and context-bound.

Marshal-by-Value Instances of marshal-by-value types can cross .NET Remoting boundaries through a process known as serialization. Serialization is the act of encoding the present state of an object into a sequence of bits. Once the object has been serialized, the .NET Remoting infrastructure transfers the sequence of bits across .NET Remoting boundaries into another application domain or context where the infrastructure then deserializes the sequence of bits into an instance of the type containing an exact copy of the state. In .NET, a type is serializable if it is declared by using the Serializable attribute. The following code snippet declares a class that's made serializable by using the Serializable attribute:

[Serializable]

class SomeSerializableClass {

In addition, a Serializable-attributed type can implement the ¡Serializable interface to perform custom serialization. We'll discuss serialization in detail in Chapter 8. Figure 2-1 shows the serialization and deserialization of an object instance from one application domain to another application domain.

Stream containing serialized object B i

Wire

Stream containing serialized object B

Figure 2-1: Marshal-by-value: object instance serialized from one application domain to another Marshal-by-Reference Marshal-by-value is fine for some circumstances, but sometimes you want to create an instance of a type in an application domain and know that all access to such an object will occur on the object instance in that application domain rather than on a copy of it in another application domain. For example, an object instance might require resources that are available only to object instances executing on a specific machine. In this case, we refer to such types as marshal-by-reference, because the .NET Remoting infrastructure marshals a reference to the object instance rather than serializing a copy of the object instance. To define a marshal-by-reference type, the .NET Framework requires that you derive from System.MarshalByRefObject. Simply deriving from this class enables instances of the type to be remotely accessible. The following code snippet shows an example of a marshal-by-reference type:

Figure 2-1: Marshal-by-value: object instance serialized from one application domain to another Marshal-by-Reference Marshal-by-value is fine for some circumstances, but sometimes you want to create an instance of a type in an application domain and know that all access to such an object will occur on the object instance in that application domain rather than on a copy of it in another application domain. For example, an object instance might require resources that are available only to object instances executing on a specific machine. In this case, we refer to such types as marshal-by-reference, because the .NET Remoting infrastructure marshals a reference to the object instance rather than serializing a copy of the object instance. To define a marshal-by-reference type, the .NET Framework requires that you derive from System.MarshalByRefObject. Simply deriving from this class enables instances of the type to be remotely accessible. The following code snippet shows an example of a marshal-by-reference type:

class SomeMBRType : MarshalByRefObject

Figure 2-2 shows how a marshal-by-reference remote object instance remains in its "home" application domain and interacts with object instances outside the home application domain through the .NET Remoting infrastructure.

Figure 2-2: Marshal-by-reference: object instance remains in its home application domain Context-Bound A further refinement of marshal-by-reference is the context-bound type. Deriving a type from System.ContextBoundObject will restrict instances of such a type to remaining within a specific context. Objects external to the containing context can't directly access ContextBoundObject types, even if the other objects are within the same application domain. We'll discuss context-bound types in detail in Chapter 6, "Message Sinks and Contexts." The following code snippet declares a context-bound type:

Figure 2-2: Marshal-by-reference: object instance remains in its home application domain Context-Bound A further refinement of marshal-by-reference is the context-bound type. Deriving a type from System.ContextBoundObject will restrict instances of such a type to remaining within a specific context. Objects external to the containing context can't directly access ContextBoundObject types, even if the other objects are within the same application domain. We'll discuss context-bound types in detail in Chapter 6, "Message Sinks and Contexts." The following code snippet declares a context-bound type:

class SomeContextBoundType : ContextBoundObject {

Figure 2-3 shows the interactions between a Context-Bound object and other objects outside its context.

Figure 2-3: Context-bound: remote objects bound to a context interact with objects outside the context through the .NET Remoting infrastructure
0 0

Post a comment