WCF - Message Contract


WCF - Message Contract   

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

Components of a service in WCF

SOAP Message Contract :

  • SOAP messages is a packet of binary data that are exchanged between the client and service in WCF. 
  • When this formatting doesn’t suit your application then Message Contract attribute can be utilized.
  • SOAP message generally comprised of Envelope, Header and Body.
  • SOAP envelope is a container for the XML namespace, and the SOAP header and SOAP body elements. 
  • SOAP Hear contains metadata information about the SOAP message. 
  • SOAP body present target information for exchange. This is depicted in the below diagram.

 SOAP Message

Message Pattern :

  • Message Pattern describes how the programs will exchange message each other. 
  • There are three way of communication between source and destination.
    • Simplex : One-way communication from source to target.
    • Request/Replay : Two-way communication between source and target, with one-way at a time.
    • Duplex : Two-way communication between both source and target.
  • Concurrent message passing happens from both end at the same instance.


One-Way and Two-Way messaging

Sample Code :

[ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [FaultContract(typeof(string))]
        AuthorResponse GetInfo(AuthorRequest Req);         
    } 
[MessageContract]
    public class AuthorRequest
    {
        [MessageHeader]
        public string AuthorId;
    }
[MessageContract]
    public class AuthorResponse
    {
        [MessageBodyMember]
        public WikiTechyAuthor Obj;
    }

Code Explanation :


    The WCF application automatically creates a Service Interface IService1.cs file on opting a new project.

    The file IService1.cs defines a MessageContract class named AuthorRequest.

  The AuthorRequest message contract has a [MessageHeader] named AuthorId.

    The AuthorResponse message contract has a [MessageBodyMember] named obj, which is an instance of the class WikiTechyAuthor.