Before explaining Message contract, I will explain about the message. Message is a unit of communication and it is in the form of a packet. These packets travel for sharing the data. It is an envelope by which the communication completes between the client and Service. This envelope consists of two parts- one is header and other is body . Header is used to send the sensitive information such as credential and token key etc.
What is Message Contract?
WCF uses soap message for communicating between the two parties. Most of the time the programmers focus on implementing the data contract, serializing the data but sometimes they need more control over the message format, then they should use message contract.
Data contract basically controls the content of the soap message such as message body but Message contract controls the structure of the message. Data contract describes what type will be exchange between the two parties and message contract describes the structure of the soap message.
Message contract is involved in controlling the structure of soap message body. It is also used to send and receive the data into soap header. The data contract and operation contract creates soap message by default but it is only the message body content. Sometimes we need more control over soap message, as we want to part our message in two portions, where one should contain the sensitive data and other should contain the message body and we can think about the message contract and this is only the choice for doing this.
Some important things that should be known to each developer about the message contract are-
- When we pass the message contract in an operation parameter, only one parameter can be passed. If we do against this rule, run time error will occur.
- The operation that has a message contract in the parameters can return either message contract type or nothing.
- The operation will return the message contract type and also accept only message contract type as a parameter.
Let’s explain with an example. Suppose, I am developing a Service, which has an operation for authenticating to the student. After successful authentication, this method will return the detail of the student. To create this service, I need to create a Student class and this class is decorated with the data contract attribute.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Web;
- namespace WcfService {
- [DataContract]
- public class Student {
- [DataMember]
- public string studentName {
- get;
- set;
- }
- [DataMember]
- public string studentId {
- get;
- set;
- }
- public string studentFees {
- get;
- set;
- } // this will not expose to client side because it does not hava DataMember Attribute
- }
- }
[OperationContract]
StudentLoginSussess LoginStudentAndGetDetail(StudentLogin login);
Token in StudentLogin class is decorated with Message Header attribute, which says this variable will come in header section of soap message and StudentName is decorated with MessageBodyMember attribute, which says it will come in body section of soap message.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- namespace WcfService {
- [MessageContract]
- public class StudentLogin {
- [MessageHeader]
- public string Token {
- get;
- set;
- }
- [MessageBodyMember]
- public string studentName {
- get;
- set;
- }
- }
- [MessageContract]
- public class StudentLoginSussess {
- [MessageBodyMember]
- public Student student {
- get;
- set;
- }
- }
- }



Asit SinghPosted Oct 14, 2016, 1:37 PM
Thanks
Bhagwant SinghPosted Oct 14, 2016, 4:37 AM
Nice artical on Message Contract In WCF .