Message Contract In WCF Service

Datetime:2016-08-23 04:36:49          Topic: WCF           Share
  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 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

<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>  

Response message

<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>  

Let us alter the message like this,

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>  

Response message

<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>  

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.


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;  
    }  
}  

StudentResponse class

[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;  
    }  
}  

Step 3:Change the service code like the following:

[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  
                };  
        }  
    }  
}  

Step 4:Make the changes in the client code like the following:

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();  

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 control 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 doubt or question in the message contract.





About List