Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataContract vs Message Contract

Though I have read some WCF articles about message contract, but I am not able to comprehend what is the real use of message contract. Can I use MessageContract instead of DataContract or in complement to DataContract?

like image 355
Vaibhav Jain Avatar asked Nov 07 '10 16:11

Vaibhav Jain


People also ask

What is a message contract?

A message contract is used to control the structure of a message body and serialization process. It is used to send/access the information in the soap header. By use of a Message Contract we can customize the parameters sent using a SOAP message between the client and the server.

What is a DataContract?

A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts.

Is DataContract mandatory in WCF?

No, the DataContractAttribute is not required - WCF will infer serialization rules.

What are the different contracts in WCF?

WCF has five types of contracts: service contract, operation contract, data contract, message contract and fault contract.


2 Answers

A DataContract is a description of a type that is exposed and supported by your service. A MessageContract is an abstraction over a SOAP message that allows you to explicitly dictate the structure of the underlying message.

They are meant to compliment each other and serve different purposes.

like image 106
Andrew Hare Avatar answered Oct 08 '22 11:10

Andrew Hare


Marc Gravell answered to a similar question WCF: MessageContract, DataContract ... Confused?

One of the advantages of message-contracts is that you can set privacy against members, but in many cases this isn't necessary. In such cases, I prefer to keep the contract as simple as possible, just as a data-contract.

like image 45
Michael Freidgeim Avatar answered Oct 08 '22 11:10

Michael Freidgeim