Message Contract In WCF Service

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:

  1. Add some custom data in the soap header. For example, add user security information or the license information.

  2. Rename the wrapper element of the SOAP message and add or remove some element from it.
So, the message contract is only used to make changes in the SOAP message. For example, consider the following sample:

For this service the client soap message will be like this,

Request message
  1. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  
  2.     <s:Header>  
  3.         <ActivityId CorrelationId="6d5cd048-ae4a-4631-b3b8-34ac8bb97400" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">4310f382-b1b2-4829-80a7-56ac725ffd80</ActivityId>  
  4.     </s:Header>  
  5.     <s:Body>  
  6.         <GetStudent xmlns="http://tempuri.org/">  
  7.             <name>sakthi</name>  
  8.         </GetStudent>  
  9.     </s:Body>  
  10. </s:Envelope>  
Response message
  1. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  
  2.     <s:Header></s:Header>  
  3.     <s:Body>  
  4.         <GetStudentResponse xmlns="http://tempuri.org/">  
  5.             <GetStudentResult xmlns:a="http://schemas.datacontract.org/2004/07/MessageContract" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">  
  6.                 <a:Department>MCA</a:Department>  
  7.                 <a:Name>Sakthi</a:Name>  
  8.                 <a:Year>3</a:Year>  
  9.             </GetStudentResult>  
  10.         </GetStudentResponse>  
  11.     </s:Body>  
  12. </s:Envelope>  
Let us alter the message like this:

Request message
  1. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  
  2.     <s:Header>  
  3.         <h:SecurityKey xmlns:h="http://localhost/schema">1234</h:SecurityKey>  
  4.         <ActivityId CorrelationId="ac7ec340-b578-4bc4-bb30-43a112d26e54" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">3d00e695-78bf-4bcb-9a41-05a2e45ef9fa</ActivityId>  
  5.     </s:Header>  
  6.     <s:Body>  
  7.         <StudentRequest xmlns="http://localhost/schema">  
  8.             <StudentName>sakthi</StudentName>  
  9.         </StudentRequest>  
  10.     </s:Body>  
  11. </s:Envelope>  
Response message
  1. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  
  2.     <s:Header></s:Header>  
  3.     <s:Body>  
  4.         <StudentResponse xmlns="http://localhost/schema">  
  5.             <Department>MCA</Department>  
  6.             <StudentName>Sakthi</StudentName>  
  7.             <Year>3</Year>  
  8.         </StudentResponse>  
  9.     </s:Body>  
  10. </s:Envelope>  
For this we need to implement the message contract. To implement the message contract in the WCF, follow the below steps.

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
  1. [MessageContract(WrapperName = "StudentRequest", IsWrapped = true, WrapperNamespace = "http://localhost/schema")]  
  2. public class StudentRequest  
  3. {  
  4.     [MessageHeader(Name = "SecurityKey")]  
  5.     public string SecurityKey  
  6.     {  
  7.         get;  
  8.         set;  
  9.     }  
  10.   
  11.     [MessageBodyMember(Name = "StudentName")]  
  12.     public string StudentName   
  13.     {  
  14.         get;  
  15.         set;  
  16.     }  
  17. }  
StudentResponse class
  1. [MessageContract(WrapperName = "StudentResponse", IsWrapped = true, WrapperNamespace = "http://localhost/schema")]  
  2. public class StudentResponse   
  3. {  
  4.     [MessageBodyMember(Name = "StudentName")]  
  5.     public string StudentName  
  6.     {  
  7.         get;  
  8.         set;  
  9.     }  
  10.   
  11.     [MessageBodyMember(Name = "Department")]  
  12.     public string Department  
  13.     {  
  14.         get;  
  15.         set;  
  16.     }  
  17.   
  18.     [MessageBodyMember(Name = "Year")]  
  19.     public int Year  
  20.     {  
  21.         get;  
  22.         set;  
  23.     }  
  24. }  
Step 3: Change the service code like the following:
  1. [ServiceContract]  
  2. public interface IStudentService  
  3. {  
  4.     [OperationContract]  
  5.     StudentResponse GetStudent(StudentRequest request);  
  6. }  
  7. }  
  8.   
  9. public class StudentService: IStudentService  
  10. {  
  11.     public StudentResponse GetStudent(StudentRequest request)  
  12.     {  
  13.         switch (request.StudentName)   
  14.         {  
  15.             case "sakthi":  
  16.                 return new StudentResponse   
  17.                 {  
  18.                     StudentName = "Sakthi", Department = "MCA", Year = 3  
  19.                 };  
  20.             case "kumar":  
  21.                 return new StudentResponse  
  22.                 {  
  23.                     StudentName = "kumar", Department = "BCA", Year = 3  
  24.                 };  
  25.             default:  
  26.                 return new StudentResponse  
  27.                 {  
  28.                     StudentName = request.StudentName, Department = "BE", Year = 3  
  29.                 };  
  30.         }  
  31.     }  
  32. }  
Step 4: Make the changes in the client code like the following:
  1. ServiceRef.IStudentService client = new ServiceRef.StudentServiceClient();  
  2. ServiceRef.StudentRequest request = new ServiceRef.StudentRequest();  
  3. request.StudentName = "sakthi";  
  4. request.SecurityKey = "1234";  
  5. ServiceRef.StudentResponse studentResponse = client.GetStudent(request);  
  6. Console.WriteLine("Name : " + studentResponse.StudentName);  
  7. Console.WriteLine("Department : " + studentResponse.Department);  
  8. Console.WriteLine("Year : " + studentResponse.Year);  
  9. Console.ReadKey();  
Note the above highlighted line. Here we used interface instead of the client class. If you want use the message contract, the communication should happen through the interface.

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.

Up Next
    Ebook Download
    View all
    Learn
    View all