Use of Message Contracts

Topics: General Discussion Forum
Oct 16, 2007 at 6:23 AM
I have always thought that I should pass data contract objects between my client and my service. When is it better to use a message contract to send a request and a response and bundle the data contract inside of the message contract?

Bill
Oct 17, 2007 at 1:47 AM
Edited Oct 17, 2007 at 1:49 AM
I am not too knowlegeable about WFC yet, but from what I have observed, message contracts are not exposed to the client as proxy classes, whereas data contracts are. Are there any other differences?


BillG wrote:
I have always thought that I should pass data contract objects between my client and my service. When is it better to use a message contract to send a request and a response and bundle the data contract inside of the message contract?

Bill


Oct 17, 2007 at 10:20 AM
The short answer is that you will use a data contract when it's OK for all of your data to go into the message body, and a message contract when you need to put data in the message header.

Message contracts incur more overhead, so don't use one unless you need to put something in the header.

James
Oct 18, 2007 at 3:42 AM
James is correct youi really use the Message contracts when you need to manipulate the header or if you want to use the XmlSerializser.

Also note messgaes contracts do not allow any other paramaters eg your method must be ResponseMessage Operation (requestMessage) this is good practice but does not allow RPC style which can be helpfull in migrating older aplications. Note however its best practice to use a DataContract to do this anyway represent a message contract and not just your data objects.

I could be wrong but I dont think James is correct on MessageContract performance . I think datacontracts are normally converted to a dynamic messagecontracts where with a MessageCOntract you have the message directly . There is a nice method on Message to convert to and from a messagecontract.

However if you use a MessageContract with the Xmlserializer than datacontracts will be faster but this is a function of the serializer . Also if a message contract has lots of properties each property is serialized in sequence. ( not good)

eg
MessageContract
public class CustomerResponse
{
MessageBodyMember private CustomerCollection customers;

}

and having CustomerCollection as a datacontract will be faster (marginally) than using a DataContract but i need to test it.


Regards,

Ben