Remoting makes an object in one process (the server) available to code in another process (the client). This is called marshalling, and there are two fundamentally different ways to marshal an object:
- Marshal by value: the server creates a copy of the object passes the copy to the client.
- Marshal by reference: the client creates a proxy for the object and then uses the proxy to access the object.
When a client makes a call to an object marshaled by value (MBV), the server creates an exact copy and sends that copy to the client. The client can then use the object's data and executable functionality directly within its own process or application domain without making additional calls to the server. To implement MBV you must either implement the ISerializable interface in your classes, or mark them with the
<serializable()> attribute.
In contrast, when a client makes a call to an object marshaled by
reference (MBR), the .NET framework creates a proxy in the client's application domain and the client uses that proxy to access the original object on the server. To implement MBR a class must, at minimum, extend the System.MarshalByRefObject class.
Figure 1 illustrates the differences between MBV and MBR.
 | |
| Figure 1. Code references remote objects either marshal by value (top) or marshal by reference (bottom). |
Given the differences between MBV and MBR, how do you decide which to use when you're programming a remotable class? In some situations you have no choiceyou must use MBR, such as when the remote component must run in its own original app domain, in order to access local files or use operating system handles. These operations could not be carried out by a copy of the object running in the client's app domain. You would also need to use MBR when the client must be made aware of changes in the object. When using MBV the copy of the object that is sent to the client is static, and does not reflect subsequent changes to the state of the object on the server.
Given the differences between MBV and MBR, how do you decide which to use when you're programming a remotable class? |
|
In many situations, however, either MBV or MBR will work, and the question is which is better? The concern here is that old bugaboobandwidth. Ask yourself which technique places fewer demands on the transport between server and client? With MBV you need move the object copy from the server to the client one time. While that can be a significant task with large objects, after the copy is at the client, calls to it are all within the client's app domain and do not involve the transport mechanism at all.
In contrast, MBR does
not require a copy of the entire object to be transported to the clientbut each and every time the client accesses the remote object it requires either a one-way or round-trip transport of information. Individually these calls may not place a heavy demand on communication, but hundreds or thousands of calls can add up.
Bottom line? Small objects that the application accesses frequently are best remoted using MBV. Large objects that the application accesses relatively infrequently are good candidates for MBR. Unfortunately, that leaves a middle ground where the choice between MBR and MBV may not be immediately obvious. You may want to do some performance testing to aid in your decision.