10 March 2014

Message Contract

       Message is the packet of data which contains important information. WCF uses these messages to transfer information from Source to destination.

      WCF uses SOAP (Simple Object Access Protocol) Message format for communication. SOAP message contain Envelope, Header and Body. SOAP envelope contains name, namespace, header and body element. SOAP Hear contain important information which are not directly related to message. SOAP body contains information which is used by the target.

      Message Pattern

        It describes how the programs will exchange message each other. There are three way of communication between source and destination
  1. Simplex - It is one way communication. Source will send message to target, but target will not respond to the message.
  2. Request/Replay - It is two way communications, when source send message to the target, it will resend response message to the source. But at a time only one can send a message
  3. Duplex - It is two way communication, both source and target can send and receive message simultaneously.
     WCF will automatically take care of message. On Some critical issue, developer will also require control over the SOAP message format. In that case WCF provides Message Contract to customize the message as per requirement.

      WCF supports either RPC(Remote Procedure Call) or Message style operation model. In the RPC model, you can develop operation with Ref and out parameter. WCF will automatically create the message for operation at run time. In Message style operation WCF allows to customize the message header and define the security for header and body of the message.

Defining Message Contract

       Message contract can be applied to type using Message Contract attribute. Custom Header and Body can be included to message using 'Message Header' and 'MessageBodyMember'atttribute'. 

       Let us see the sample message contract definition.

    [Message Contract]
    public class EmployeeDetails
     {
         [Message Header]
         public string EmpID;
         [MessageBodyMember]
         public string Name;
         [MessageBodyMember]
         public string Designation;
         [MessageBodyMember]
         public int Salary;
         [MessageBodyMember]
         public string Location;
    }

   When we use this Employee Details type in the service operation as parameter. WCF will add extra header call 'EmpID' to the SOAP envelope. It also add Name, Designation, Salary, Location as extra member to the SOAP Body.

Points to Remember 
  •  When using Message contract type as parameter, Only one parameter can be used in service Operation
  •   Service operation either should return Message contract type or it should not return any value       
  •  Service operation will accept and return only message contract type. Other data types are not allowed.

      

No comments:

Post a Comment