Thursday 26 May 2016

Message Contract in WCF? What is MessageContract in WCF?



DataContract has limited control over SOAP message and all the control related to content inside the body of the SOAP message. But sometime scenario comes, when we need more control over SOAP message. So in that scenario we can use Message Contract.

Message contract describe the structure of SOAP message sent to and from the service and enable you to inspect and control most of the details in the SOAP header and body.

Consider a scenario; an authentication code is required to access our service. In this case functionality provided by our service will same but authentication code validity is additional pre-requisite. So in that case, SOAP message header is most reasonable place to store the authentication code while calling the service method.

Example:-
[MessageContract]
public class StudentDetails
{
    [MessageHeader]
    public string EmpId;
    [MessageBodyMember]
    public string StudentName;
    [MessageBodyMember]
    public string Age;
    [MessageBodyMember]
    public string Course;
    [MessageBodyMember]
    public string Address;
}

Note:- If both type of Contract is present in service , then service operation will accept only message contract.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.