18.1 Creating a Remotable Object

 <  Day Day Up  >  

You want to create an object that is called across different application domains, such as across the network.


Technique

The only requirement to create an object that is accessed across the network is to derive it from the base class System.MarshalByRefObject :

 
 public class RemoteObject : MarshalByRefObject {     public void RemoteObject()     {         Console.WriteLine("Constructor RemoteObject");     }     public string Greeting(string name)     {         Console.WriteLine("Greeting");         return "Hello, " + name;     } } 

Comments

When you derive the remotable object from the class MarshalByRefObject , the object is bound to the application domain, and a client accessing the object from a different application domain has to use a proxy. All public methods and properties of this class are accessed by using a proxy. Static methods are not accessed across the network.

Figure 18.1 shows a client calling methods of the proxy. The proxy passes the method call into a channel that is responsible for sending the call across the network. On the server side, a dispatcher invokes the method in the remote object.

Figure 18.1. A client calling methods of the proxy.

graphics/18fig01.gif

What's important with a remote object is that you have to be aware of state management. Remotable objects can be stateless or stateful ”depending on how the objects are configured with the server. You just have to be aware because it influences the implementation of the remote object class.

With stateless objects, you can only use a default constructor. With client-activated stateful objects, you can use specialized constructors. Client-activated objects are discussed in Recipe 18.4, "Performing Client-Side Activation of Remotable Objects."

For parameters of remote methods, if you want to serialize an object across the network, from the server to the client or the client to the server, the class must be marked with the attribute class SerializableAttribute . Classes that derive from MarshalByRefObject are passed by reference, and the object stays in the application domain where it was created. You use a proxy from the other side of the network to access the object. If a class is not marked Serializable or derived from MarshalByRefObject , it cannot be used as a parameter of a remote method.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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