Tuesday 13 December 2016

Message Contract in WCF

A Message Contract is used to control the structure of a message body and serialization process. It is also used to send / access information in SOAP headers. By default WCF takes care of creating SOAP messages according to service DataContracts and OperationContracts. 

 
A MessageContract controls the structure of a message body and serialization process. A MessageContract can be typed or untyped and are useful in interoperability cases and when there is an existing message format we must comply with. 

When an operation contract required to pass a message as a parameter or return value as a message, the type of this message will be defined as message contract. A message contract defines the elements of the message (like as Message Header, Message Body), as well as the message-related settings, such as the level of message security.

Message contracts give you complete control over the content of the SOAP header, as well as the structure of the SOAP bod

The SOAP header is implemented in the namespace system.web.services.protocol.
  1. [MessageContract]  
  2. public class AutherRequest  
  3. {  
  4.    public string AutherId;  
  5. }  
Message Header

A Message Header is applied to a member of a message contract to declare the member within the message header.
  1. [MessageContract]  
  2. public class AutherRequest  
  3. {  
  4.    [MessageHeader]  
  5.    public string AutherId;  
  6. }  
Message Body Member

A Message Body Member is applied to the member of a message contract to declare the members within the message body.
  1. [MessageContract]  
  2. public class AuthorResponse  
  3. {  
  4.     [MessageBodyMember]  
  5.     public Auther Obj;  
  6. }  

No comments:

Post a Comment