Message Contract in WCF

Before discussion about message contract, I want to explain data contracts, messages and envelopes. In WCF a data contract enables us to define the structure of the data. This data is sent in the body of an envelope. 

MessageWCF1.jpg

A message is nothing but a packet and WCF uses this packet to transfer the information from source to destination. This message is contained in the header or body.

A message contract is used to control the structure of a message body and serialization process. It is used to send/access the information in the soap header. By use of a Message Contract we can customize the parameters sent using a SOAP message between the client and the server. The SOAP header is implemented in the namespace system.web.services.protocol.

[MessageContract]

public class AutherRequest

{

public string AutherId;

 

}

Now you want to know: What are MessageHeader and MessageBodyMember?

Message Header

A Message Header is applied to a member of a message contract to declare the member within the message header.

[MessageContract]

public class AutherRequest

{

[MessageHeader]

public string AutherId;

 

}

Message Body Member

A Message Body Member is applied to the member of a message contract to declare the members within the message body.
[MessageContract]

public class AuthorResponse

{

[MessageBodyMember]

public Auther Obj;

 

}

Confuse when you go for Message Contract and when for Data Contract

Data contracts are used to define the data structure and generate the XML for the data you want to pass. If you want to go for more control on your SOAP Message then you should go for a Message Contract. Do not mix message contract and data contract.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s