WebReference.com - Part 3 of Chapter 6: Professional C# Web Services, from Wrox Press Ltd (2/5)

To page 1current pageTo page 3To page 4To page 5
[previous] [next]

Professional C# Web Services

Marshaling

Marshaling is the term used when an object is converted so that it can be sent across the network (or across processes or application domains). Unmarshaling creates an object from the marshaled data.

With marshal-by-value (MBV) the object is serialized into the channel, and a copy of the object is created on the other side of the network. The object to marshal is stored into a stream, and the stream is used to build a copy of the object on the other side with the unmarshaling sequence. Marshaling-by-reference (MBR) creates a proxy on the client that is used to communicate with the remote object. The marshaling sequence of a remote object creates an ObjRef instance that itself can be serialized across the network.

Objects that are derived from MarshalByRefObject are always marshaled by reference – as the name of the base class says. To marshal a remote object the static method RemotingServices.Marshal() is used.

RemotingServices.Marshal() has these overloaded versions:
public static ObjRef Marshal(MarshalByRefObject obj);
public static ObjRef Marshal(MarshalByRefObject obj, string objUri);
public static ObjRef Marshal(MarshalByRefObject obj, string objUri, 
       Type requestedType);

The first argument obj specifies the object to marshal. The objUri is the path that is stored within the marshaled object reference; it can be used to access the remote object. The requestedType can be used to pass a different type of the object to the object reference. This is useful if the client using the remote object shouldn't use the object class but an interface that the remote object class implements instead. In this scenario the interface is the requestedType that should be used for marshaling.

ObjRef

With all these Marshal() methods an ObjRef is returned. The ObjRef is serializable because it implements the interface ISerializable, and can be marshaled by value. The ObjRef knows about the location of the remote object: the host name, the port number, and the object name. RemotingServices.Unmarshal() uses the ObjRef to create a proxy that will be used by the client.

Passing Objects

With methods of remote objects we can pass basic data types and classes that we define. Whether an object is passed by value or by reference depends on the class declaration, as we will see next. We will add two methods to the class MyRemoteObject that return objects of class A and B. Class A will be serializable and passed using MBV to the client, B will be remotable and passed using MBR.

Passing objects between client and server

Let's start by adding two methods to the remote object class MyRemoteObject in the file MyRemoteObject.cs that returns objects of class A and B with the methods GetA() and GetB(). We will create the classes A and B in the next section:

// MyRemoteObject.cs
using System;
namespace Wrox.Samples
{
   public class MyRemoteObject : System.MarshalByRefObject
   {
      public MyRemoteObject()
      {
         Console.WriteLine("MyRemoteObject constructor called");
      }
      // serialized version
      public A GetA()
      {
         return new A(11);
      }
      // remote version
      public B GetB()
      {
         return new B(22);
      }
 
      public string Hello()
      {
         Console.WriteLine("Hello called");
         return "Hello, .NET Client!";
      }
   }
}

To page 1current pageTo page 3To page 4To page 5
[previous] [next]

Created: March 26, 2002
Revised: March 26, 2002


URL: http://webreference.com/programming/csharp/webservices/chap6/3/2.html