Remoting (.NET 1.1) Performance Guidelines - Serialization and Marshaling

From Guidance Share

Jump to: navigation, search

- J.D. Meier, Srinath Vasireddy, Ashish Babbar, and Alex Mackman


Consider Using a Data Facade

Consider using a data facade to wrap the most relevant data needed by the client. You can develop a wrapper object, with a coarse-grained interface, to encapsulate and coordinate the functionality of one or more objects that have not been designed for efficient remote access. It provides clients with single interface functionality for multiple business objects.

Alternatively, instead of making a remote call to fetch individual data items, you can fetch a data object by value in a single remote call. When you do that, you operate locally against the locally cached data. This might be sufficient for many scenarios.

In other scenarios, where you need to ultimately update the data on the server, the wrapper object exposes a single method which you call to send the data back to the server.

Marshal Data Efficiently and Prefer Primitive Types

A method call across a remoting boundary is expensive and slow, in comparison to in-process method calls. Make sure that you pass only the data that you need. Avoid passing data that can be simply recalculated.

.NET serialization support makes serializing an object graph very easy by using the Serializable attribute. However, if you overuse this attribute, you ensure that a large amount of extra data is passed when simpler data types would frequently suffice. Try to return value types, such as simple primitive types or structures, first. You should consider using more complex types only if these simple types are not sufficient. A classic example of extreme inefficiency is to return a DataSet that has been populated with a single row, single column value when an integer would do.

Reduce Serialized Data by Using NonSerialized

Serialize only the required data. You can reduce the amount of object state that is serialized by marking specific fields that you do not need to serialize with the NonSerialized attribute as follows.

  Class MyObject: MarshalByRefObject
    Private DataSet dt;

Note You need to use the NonSerialized attribute on both public and private fields.

Prefer the BinaryFormatter

The BinaryFormatter produces a compact data representation and should be preferred, unless you have a specific requirement for SOAP, in which case you should use Web services. You can use the BinaryFormatter with both the TcpChannel and HttpChannel.


For more information about improving the serialization performance, see How To Improve Serialization Performance.

Personal tools