Understanding Remotable Objects

Just as we make the distinction between value types and reference types in managed code, we refer to the objects that are exposed by remote servers as either marshal-by-value or marshal-by-reference objects. This specifies how object state and instance data is passed over the Remoting channel. In this section, you will learn about both types of remotable objects.

Marshal-by-Value Object

When a marshal-by-value object is passed between components, a complete copy of the object is serialized and passed through the Remoting channel to the caller. The object can then be transparently re-created in the caller’s process by the Remoting infrastructure so the caller can use the object. All subsequent calls on the object or accesses of the object’s properties are done within the caller’s process. marshal-by-value objects are created by marking the class with the <Serializable> attribute or by implementing the ISerializable interface in the source class and creating a custom serialization method.

When objects are passed as parameters, they are often passed as marshal-by-vlue. The ADO.NET DataSet class is an example of a common .NET Framework object that is serialized and copied whenever it is passed from one component to another. Although copying and re-creating the entire description of an object might take some time, slowing down the first call to the object, it can sometimes be more efficient than making several round-trips between client and server when you expect to be making multiple calls to the object.

The following code snippet shows a class declaration that uses attributes. The <Serializable> attribute marks the class as a whole as able to be written out to an XML stream and transmitted to another component. The <NonSerialized> attribute can be applied to individual members that will not be included when the object’s state is passed to the caller.

Here is an example:

' An object that can be serialized <Serializable()> Public Class myByValueObject        Public variable1 As Integer    Public variable2 As String        ' A member that is not passed to the caller    <NonSerialized()> Public variable3 As String  

Marshal-by-Reference Object

When a marshal-by-reference object is passed between components, a proxy object is created in the caller’s process. This object is a stand-in for the remote object, it shows the client the same interface as the remote object and allows the client code to make method calls as though it were calling a local object. When the caller makes method calls on the proxy object, the .NET Remoting infrastructure passes those calls to the remote server, and the call is carried out in the server’s process. Marshal-by-reference objects are created by inheriting System.MarshalByRefObject in the source class. You should use marshal-by-reference objects when the object is dependent on using resources that can be accessed only from the object’s original application domain (such as files located on a specific computer).

As we have mentioned, there is a trade-off between the time required to serialize an object and pass it in its entirety to the caller, and the total number of calls made to the object. If your server objects are very large and the caller is likely to be making only one call on the object, it is more efficient to use marshal-by-reference.

The following code snippet shows a class declaration that inherits MarshalByRefObject:

Public Class ServiceClass    Inherits MarshalByRefObject



MCAD/MCSD(c) Visual Basic. NET XML Web Services and Server Components Study Guide
MCAD/MCSD: Visual Basic .NET XML Web Services and Server Components Study Guide
ISBN: 0782141935
EAN: 2147483647
Year: 2005
Pages: 153

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net