Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 11 - Difference between datacontract and messagecontract in wcf

Suggested Videos
Part 8 - Different ways of associating known types in wcf
Part 9 - How to enable tracing and message logging in wcf
Part 10 - Message Contract in WCF

DataContract gives very limited control over the SOAP messages. DataContract allows us to control the Name and Order of XML elements in the body section of the SOAP message. Beyond this we don't have much control over the SOAP messages.

On the other hand, MessageContract gives full control over the SOAP messages by providing access to the SOAP header and body sections using MessageHeader and MessageBodyMember attributes. Use MessageContract if there is a reason to tweak the structure of the soap XML i.e if you want to include any additional information in the SOAP header.

WCF Interview Questions related to MessageContract that were asked in an interview attended by one of our youtube subscribers
1. Why do use MessageContract in WCF?
MessageContract gives full control over the SOAP messages. For example, it allows us to include custom information in the SOAP header.

2. What kind of custom information?
User credentials to invoke the service.

3. Why do you need to pass user credentials in the header? Can't you pass them as method parameters?
We can, but user credentials are periphery to what the method has to do. So, it would make more sense to pass them out of band in the header, rather than as additional parameters.

4. SOAP messages are in xml format, so anyone can read the credentials? How you do you protect sensitive data?
Using MessageContract we can sign and encrypt messages. Use ProtectionLevel named parameter.

wcf tutorial


  1. sir i want to know how to store and retrieve the image from sqlserver

  2. Thanks for making this wonderful blog . how can I host WCFservice in IIS .please give a video tutorial..

  3. Thanks heaps for this video series.Its really awesome.Where can i find the code samples used in this video ?


If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.