When is one used and when is another? They look very similar in appearance.
1 answer
In outline:
Agreements ( Contracts ) in WCF provide the compatibility needed to interact with the client. DataContract and MessageContract are structural contracts that complement each other and serve different purposes.
DataContract is an agreement between the parties (the service and the client) that describes the type of data they will exchange, in other words, the DataContract used to define the structure of the message data, i.e. DataContract determines which parameters and return types will be serialized / deserialized Binary <==> XML for exchange between the parties.
WCF uses SOAP messages to communicate. MessageContract used to control the structure of the SOAP SOAP message body ( SOAP message body ) and data serialization, as well as to transfer information in the SOAP message headers ( SOAP header ).
Thus, using MessageContract preferable only when there is a need to control the “layout” of your message ( SOAP message ). For example, add specific data to the SOAP Header messages .
Total:
In 90% of cases, using DataContract will be enough to achieve your goals, but if you need to very carefully control the "layout" of your SOAP message , then MessageContract comes to the MessageContract .