Accessing Remote Class Types


To access a remote class type in another application domain, you have to make a class remotable . Making a class remotable requires telling the .NET remoting infrastructure to marshal the class type in order to access it across application domain boundaries. Marshaling is a process of packaging up a class in one application domain, sending it to another application domain, and unpackaging the class for use in the other application domain. The two styles of marshaling are marshal by value and marshal by reference .

Marshal by Value

Marshal by value occurs when a class type is copied from one application domain to another. When a calling application domain references a class in another application domain, the caller receives a copy of the class from the remoting infrastructure and the caller can access only the methods and properties of the copied class in the calling application domain. Marshal by value is useful when you need to pass a data structure, such as an array of strings, from one application domain to another.

To marshal a class by value requires adding the Serializable attribute to a class definition as shown in the following code:

C#

 [Serializable] publicclassMyData { publicintField1; publicstringField2 } 

Visual Basic .NET

 <Serializable()>_ PublicClassMyData PublicField1AsInteger PublicField2AsString EndClass 

When a class type is passed from one application domain to another, adding the Serializable attribute informs the .NET remoting infrastructure to package up and copy the class type to another application domain. For more information on how class types are serialized, see Chapter 4.

Marshal by Reference

Marshal by reference occurs when you remotely access the properties and methods of a class in another application domain. To marshal a class by reference requires that a class inherit the MarshalByRefObject class. The following code demonstrates a class named DemoClass that can be marshaled by reference across application domains.

C#

 namespaceDemo { publicclassDemoClass:MarshalByRefObject { publicintm_TheValue=0; publicvoidPrintMessage(stringMessage) { Console.WriteLine(Message); } } } 

Visual Basic .NET

 NamespaceDemo PublicClassDemoClass:InheritsMarshalByRefObject Publicm_TheValueAsInteger=0 PublicSubPrintMessage(ByValMessageAsString) Console.WriteLine(Message) EndSub EndClass EndNamespace 

Marshal by reference allows an application on computer A to invoke class methods and access class properties on computer B. For example, if there is an application on computer A that references DemoClass on computer B, then computer A can call PrintMessage and make the method run on computer B.

The .NET remoting architecture performs marshal by reference by setting up a transparent proxy in the calling application domain that handles making method calls and accessing properties. The proxy makes the remote class look as if it is available in the clients application domain when in reality it is running in another application domain. A proxy is established when an application instantiates the reference to the remote class. Once a proxy is established it will communicate with a peer by passing messages across a .NET remoting channel. Well discuss remoting channels in more detail later in the chapter.

A major difference between marshal by value vs. marshal by reference is that marshal by value does not allow you to access remote properties and run remote methods. Instead, if you have a remote method (marshal by reference) that returns a class type, the class type must be marked with the Serializable attribute (marshal by value) so that the return type can be copied from a remote domain to your local domain.




Network Programming for the Microsoft. NET Framework
Network Programming for the MicrosoftВ® .NET Framework (Pro-Developer)
ISBN: 073561959X
EAN: 2147483647
Year: 2003
Pages: 121

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