Let's see what message contract is. The message contract is used to control the soap message between the WCF service and the client communication:
- Add some custom data in the soap header. For example, add user security information or the license information.
- Rename the wrapper element of the SOAP message and add or remove some element from it.
For this service the client soap message will be like this,
Request message
- <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
- <s:Header>
- <ActivityId CorrelationId="6d5cd048-ae4a-4631-b3b8-34ac8bb97400" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">4310f382-b1b2-4829-80a7-56ac725ffd80</ActivityId>
- </s:Header>
- <s:Body>
- <GetStudent xmlns="http://tempuri.org/">
- <name>sakthi</name>
- </GetStudent>
- </s:Body>
- </s:Envelope>
- <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
- <s:Header></s:Header>
- <s:Body>
- <GetStudentResponse xmlns="http://tempuri.org/">
- <GetStudentResult xmlns:a="http://schemas.datacontract.org/2004/07/MessageContract" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
- <a:Department>MCA</a:Department>
- <a:Name>Sakthi</a:Name>
- <a:Year>3</a:Year>
- </GetStudentResult>
- </GetStudentResponse>
- </s:Body>
- </s:Envelope>
Request message
- <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
- <s:Header>
- <h:SecurityKey xmlns:h="http://localhost/schema">1234</h:SecurityKey>
- <ActivityId CorrelationId="ac7ec340-b578-4bc4-bb30-43a112d26e54" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">3d00e695-78bf-4bcb-9a41-05a2e45ef9fa</ActivityId>
- </s:Header>
- <s:Body>
- <StudentRequest xmlns="http://localhost/schema">
- <StudentName>sakthi</StudentName>
- </StudentRequest>
- </s:Body>
- </s:Envelope>
- <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
- <s:Header></s:Header>
- <s:Body>
- <StudentResponse xmlns="http://localhost/schema">
- <Department>MCA</Department>
- <StudentName>Sakthi</StudentName>
- <Year>3</Year>
- </StudentResponse>
- </s:Body>
- </s:Envelope>
Step 1:
Create two class named StudentRequest and StudentResponse. The StudentRequest class used to send the request to WCF service and StudentResponse class used to get the response back from the WCF service.
Step 2: Add the following code in the classes.
StudentRequest class
- [MessageContract(WrapperName = "StudentRequest", IsWrapped = true, WrapperNamespace = "http://localhost/schema")]
- public class StudentRequest
- {
- [MessageHeader(Name = "SecurityKey")]
- public string SecurityKey
- {
- get;
- set;
- }
- [MessageBodyMember(Name = "StudentName")]
- public string StudentName
- {
- get;
- set;
- }
- }
- [MessageContract(WrapperName = "StudentResponse", IsWrapped = true, WrapperNamespace = "http://localhost/schema")]
- public class StudentResponse
- {
- [MessageBodyMember(Name = "StudentName")]
- public string StudentName
- {
- get;
- set;
- }
- [MessageBodyMember(Name = "Department")]
- public string Department
- {
- get;
- set;
- }
- [MessageBodyMember(Name = "Year")]
- public int Year
- {
- get;
- set;
- }
- }
- [ServiceContract]
- public interface IStudentService
- {
- [OperationContract]
- StudentResponse GetStudent(StudentRequest request);
- }
- }
- public class StudentService: IStudentService
- {
- public StudentResponse GetStudent(StudentRequest request)
- {
- switch (request.StudentName)
- {
- case "sakthi":
- return new StudentResponse
- {
- StudentName = "Sakthi", Department = "MCA", Year = 3
- };
- case "kumar":
- return new StudentResponse
- {
- StudentName = "kumar", Department = "BCA", Year = 3
- };
- default:
- return new StudentResponse
- {
- StudentName = request.StudentName, Department = "BE", Year = 3
- };
- }
- }
- }
- ServiceRef.IStudentService client = new ServiceRef.StudentServiceClient();
- ServiceRef.StudentRequest request = new ServiceRef.StudentRequest();
- request.StudentName = "sakthi";
- request.SecurityKey = "1234";
- ServiceRef.StudentResponse studentResponse = client.GetStudent(request);
- Console.WriteLine("Name : " + studentResponse.StudentName);
- Console.WriteLine("Department : " + studentResponse.Department);
- Console.WriteLine("Year : " + studentResponse.Year);
- Console.ReadKey();
The main difference between the data contract and the message contract is, the data contract controls the SOAP message partially. But message contract provides the full control over the SOAP message
That’s all, we will make any changes in the soap message using the message contract. Please feel free to comment, if you have any doubts or questions about the message contract.

Raja TPosted Jan 20, 2016, 10:53 PM
Nice, Thanks for sharing
Arul RPosted Jan 20, 2016, 12:44 PM
Nice share
Debasis SahaPosted Jan 20, 2016, 12:19 AM
Nice article..
Santhakumar MunuswamyPosted Jan 19, 2016, 2:52 PM
Thanks for nice article. keep it up
Mohammed IbrahimPosted Jan 19, 2016, 9:00 AM
nice
Humayun Kabir MamunPosted Jan 19, 2016, 7:50 AM
Nice...
Jaipal ReddyPosted Jan 19, 2016, 1:07 AM
Nice. .