Partilhar via


Custom Message Formatters

The content in a message is often in the form of XML, which is usually not a convenient format for an application. Applications manipulate objects, getting and setting their properties. Windows Communication Foundation (WCF) uses the Data Contract to convert a Message object into an object easily handled by an application. These processes are called serialization and deserialization. Note that these same terms are used to describe the serialization and deserialization done by the transport layer to and from the message wire format, which is an unrelated process.

You can use a custom message formatter if you need to implement a specialized conversion between messages and objects that you cannot accomplish by means of a Data Contract. Do this by modifying or extending the execution behavior of a specific contract operation on a client or a service.

Custom Message Formatters on the Client

The IClientMessageFormatter interface defines methods that are used to control the conversion of messages into objects and objects into messages for client applications.

You must implement this interface. First override the DeserializeReply method to deserialize a message. This method is called after an incoming message is received, but before it is dispatched to the client operation.

Next, override the SerializeRequest method to serialize an object. This method is called prior to sending an outgoing message.

To insert the custom formatter into the service application, assign the IClientMessageFormatter object to the Formatter property using an operation behavior. For information about behaviors, see Configuring and Extending the Runtime with Behaviors.

Custom Message Formatters on the Service

The IDispatchMessageFormatter interface defines methods that convert a Message object into parameters for an operation and from parameters into a Message object in a service application.

You must implement this interface. First override the DeserializeReply method to deserialize a message. This method is called after an incoming message is received, but before it is dispatched to the client operation.

Next, override the SerializeRequest method to serialize an object. This method is called prior to sending an outgoing message.

To insert the custom formatter into the service application, assign the IDispatchMessageFormatter object to the Formatter property using an operation behavior. For information about behaviors, see Configuring and Extending the Runtime with Behaviors.

See Also

Reference

IClientMessageFormatter
IDispatchMessageFormatter

Concepts

Configuring and Extending the Runtime with Behaviors