July 8, 2007, 1:49 p.m.
posted by vendetta
An Overview of Remoting
In addition to sending class data among network devices, the .NET architecture provides a technique for sending entire class method information across the network. Similar to web services, which was introduced in Chapter 14, remoting allows you to share C# class methods between machines on a network. Unlike web services, remoting does not require any additional server software to process incoming requests. It is easy to use .NET remoting to incorporate distributed computing in your network applications.
The idea behind remoting is to utilize computing resources on other systems on the network. Remoting enables you to write applications that provide application services to anyone on the network. You’ll need five elements to build a .NET remoting application:
-
A remoting class that provides C# methods used by client applications on the network.
-
A remoting server, which hosts the remoting class, allowing network clients to connect and process method calls.
-
A communication channel that passes method calls from the network client to the remoting server and returns the results back to the network client.
-
A proxy class, which runs on the client machine. The proxy class accepts method calls from the client and passes them through the communication channel to the remote class.
-
A client application, which passes method calls to the proxy class, and interprets information returned from the remoting class through the proxy class.
Figure shows how the .NET remoting pieces fit together.
The Remote Class
The remote class used in .NET remoting provides the data elements and methods advertised by the remoting server to clients. Until now, all of the programs were contained in a single application domain. An application domain is defined as an area where all of the applications share the same memory space. Any class that is outside of the application domain of a client is considered a remote class. This is shown in Figure.
Also bear in mind that remote classes don’t necessarily have to be on remote devices. A single workstation can contain multiple application domains. Instead of a physical barrier between the domains (such as a network), a security barrier is present, with the operating system’s security rules providing the separation between the application domains. When classes are outside the application domain, they cannot directly access each other by reference (there is no shared memory area). Instead, each class instance must be serialized and passed through a communication channel to the other client application.
The Remoting Server
The remoting server is a C# application program that interfaces with the .NET remoting system and the remote class. It does this by registering the remote class in the application domain on the machine and listening for incoming requests on a specific communication channel. There are two techniques with which the remoting server can process incoming requests on the remote class:
SingleCall object mode The SingleCall mode creates a new instance of the remote class object for each incoming client request. When the client connection is terminated, the class instance is deleted. No state information can be shared between client requests. The SingleCall object mode is used when it is important to separate information between class instances.
Singleton object mode In contrast, the Singleton mode uses one instance of the remote class object to handle all incoming requests. Singleton mode allows you to build server applications that have persistent data between client connections on the server. The Singleton mode object has a set lifetime to hold the remote class state information. When the lifetime expires, the remote class object is deleted. If a new client request is received, a new remote class object is created. The Singleton object mode is used when it is important to share class instances for performance reasons.
The Communication Channel
The .NET remoting system relies heavily on communication channels to pass remote class information to clients. As each client accesses a remote class, information about the class methods, method parameters, and method return values must be passed between the devices. This can occur via raw TCP communication, which passes serialized class information directly using TCP on a designated port. Or an HTTP communication channel can be used, which provides a standard communication technique for passing data between the client and the remote class.
| Warning |
When using multiple remoting classes on a single device, you must remember that only one communication per port per channel can be used. |
The Proxy Class
The proxy class on the client machine represents the remote class methods that can be accessed on the remoting server. The proxy class offers an interface for the client programs to access the remote classes running on the remote server.
It is the responsibility of the proxy class to serialize the data and pass it to the remote class server. Conversely, it is also the proxy class’s responsibility to deserialize any incoming data and place it in the proper class data elements for the client programs.
Without the proxy class, the client application would not know how the class data elements are formatted, or what datatypes to use for the data elements.
The Client Program
For a client program to use the class methods in the remote class, the client must know what methods are available and how to access them. The client program must be compiled with a proxy class to determine the format of the remote class methods. All references to the remote class methods are passed to the proxy class for processing.
The client program has two options for contacting the remote class methods, to register a communication channel to use for the remote class itself and handle all communication with the remote class, using the proxy class for the remote class method information, or to allow the proxy class to register the communication channel and handle all of the communication.
Both techniques have their pros and cons. If you allow the proxy class to handle all of the communication channel requirements, the client program becomes less complicated, but it is tied to one specific communication channel. On the other hand, if the client program handles the communication channel, it can be changed as necessary if the remote class moves to a different server.

