Skip to content

Latest commit

 

History

History
37 lines (23 loc) · 3.68 KB

custom-message-formatters.md

File metadata and controls

37 lines (23 loc) · 3.68 KB
description title ms.date ms.assetid
Learn more about: Custom Message Formatters
Custom Message Formatters
03/30/2017
c07435f3-5214-4791-8961-2c2b61306d71

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 xref:System.ServiceModel.Channels.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 xref:System.ServiceModel.Dispatcher.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 xref:System.ServiceModel.Dispatcher.IClientMessageFormatter.DeserializeReply%2A 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 xref:System.ServiceModel.Dispatcher.IClientMessageFormatter.SerializeRequest%2A 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 xref:System.ServiceModel.Dispatcher.IClientMessageFormatter object to the xref:System.ServiceModel.Dispatcher.ClientOperation.Formatter%2A property using an operation behavior. For information about behaviors, see Configuring and Extending the Runtime with Behaviors.

Custom Message Formatters on the Service

The xref:System.ServiceModel.Dispatcher.IDispatchMessageFormatter interface defines methods that convert a xref:System.ServiceModel.Channels.Message object into parameters for an operation and from parameters into a xref:System.ServiceModel.Channels.Message object in a service application.

You must implement this interface. First override the xref:System.ServiceModel.Dispatcher.IClientMessageFormatter.DeserializeReply%2A 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 xref:System.ServiceModel.Dispatcher.IClientMessageFormatter.SerializeRequest%2A 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 xref:System.ServiceModel.Dispatcher.IDispatchMessageFormatter object to the xref:System.ServiceModel.Dispatcher.DispatchOperation.Formatter%2A property using an operation behavior. For information about behaviors, see Configuring and Extending the Runtime with Behaviors.

See also