MessageContract Vs DataContract

By Mithun Bose at February 07, 2010 19:55
Filed Under: .NET Framework, WCF

For anyone new to WCF, this probably is the most common question they get. Why do we need MessageContract when DataContract can do the job?

A very simple answer to the question is, when you need a higher level of control over the message, such as sending custom SOAP header, you then use MessageContract instead of DataContract. But in my opinion, most of the messaging needs can be catered by DataContracts.

This is the extract from MSDN : Sometimes complete control over the structure of a SOAP message is just as important as control over its contents. This is especially true when interoperability is important or to specifically control security issues at the level of the message or message part. In these cases, you can create a message contract that enables you to use a type for a parameter or return value that serializes directly into the precise SOAP message that you need.

To elborate on the above mentioned MSDN extract, a message contract allows you to specifically control which elements will be in the SOAP header, and which will be in the SOAP body and this isn't possible using the DataContract. As DataContracts represent types. Messages are not used as types but rather the payload a method operates on and they are specific to the operation they are passed to and from.

This situation arises when you have a communication partner which requires a very specific format and you have to tweak your SOAP messages to match that given layout exactly. In my view, always use DataContracts unless you have to use MessageContract for a very good reason.

One of the hypothetical situation could be your communication partner would like to have a custom security header with username and hashed password. So you could have a message contract something similar to the below

 

[MessageContract]
public class BankingTransaction
{
  [MessageHeader] public string UserName;
  [MessageHeader] public string Password;
  [MessageBodyMember] private Account sourceAccount;
  [MessageBodyMember] private Account targetAccount;
  [MessageBodyMember] public int amount;

}

Notice on the above contract the UserName and Password are decorated with MessageHeader attribute. The generated SOAP message with WCF basicHttpBinding will look like following:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" 
xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">
http://tempuri.org/IService1/MoveAccountData
</Action>
    <h:Password xmlns:h="http://tempuri.org/">HashPassword</h:Password>
    <h:UserName xmlns:h="http://tempuri.org/">TestUser</h:UserName>
  </s:Header>
  <s:Body>
    <BankingTransaction xmlns="http://tempuri.org/">
      <amount>10</amount>
      <sourceAccount>1234</sourceAccount>
      <targetAccount>5678</targetAccount>
    </BankingTransaction>
  </s:Body>
</s:Envelope>

 

Notice the SOAP the password and username members are serialized as SOAP header and the remaining members were serialized as SOAP body.

This was just one of the e.g. of the level of control that could be achieved by MessageContract. There are other controls such as encrypting or signing part of the messages, but that could be a blog post for another day.

 

 

Comments

3/8/2010 6:21:21 AM #

moon in my room

Thanks for taking this opportunity to talk about this, I feel strongly about it and I benefit from learning about this subject. If possible, as you gain data, please update this blog with new information. I have found it extremely useful.

moon in my room United States |

3/10/2010 5:40:45 PM #

Idell Narasimhan

Hello - I must say, I’m impressed with your site. I had no trouble navigating through all the tabs and information was very easy to access. I found what I wanted in no time at all. Pretty awesome. Would appreciate it if you add forums or something, it would be a perfect way for your clients to interact. Great job

Idell Narasimhan United States |

3/13/2010 11:39:43 AM #

horde leveling guide

Admiring the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my children check up here often. Thumbs up!

horde leveling guide United States |

3/13/2010 12:13:49 PM #

horde leveling guide

Admiring the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my children check up here often. Thumbs up!

horde leveling guide United States |

3/20/2010 2:25:46 AM #

Rashad Posey

Though I would've loved it much more if you added a relevant video or at least pictures to back up the explanation, I still thought that your write-up quite helpful. It's usually hard to make a complicated matter seem very easy. I enjoy your weblog and will sign up to your feed so I will not miss anything. Fantastic content

Rashad Posey United States |

3/26/2010 2:49:38 AM #

privat Krankenversicherung

Please, can you inform me few a lot more thinks around this; I'm truly lover of one's web log...

privat Krankenversicherung Austria |

3/26/2010 4:06:54 AM #

Mackey J. McDonald

Super-Duper site! I am loving it!! Will come back again - taking you feeds also, Thanks.

Mackey J. McDonald United States |

About the author

I am a professional software developer based in London, England. I specialize in developing enterprise application and integration framework in C#, SQL Server, ASP.NET. 

My area of interests include SOA using WCF, Web, Business Intelligence and Database programming. I am a Computer Science Graduate and Microsoft Certified Professional Developer in enterprise application.