[MessageBodyMember] Attribute in WCF

The [MessageBodyMember] attribute identifies the members who are to be serialized as a SOAP body element:

[MessageContract]
public class BookOrder
{
[MessageHeader]
public string ISBN
[MessageBodyMember]
public int Quantity

This code snippet defines a message contract, as well a message header and a message body member.
The value of ISBN will be a data member as a SOAP message header, and the value of Quantity will be serialized into the SOAP body.

Name

The Name property of the [MessageBodyMember] attribute defines the name of the element for this member:

[MessageContract]
public class BookOrder
{
[MessageHeader]
public string ISBN
[MessageBodyMember(Name=”BookOrderQuantity”]
public int Quantity

This property must follow XML standards and be a valid XML element. For example, in this code snippet, the Name parameter is applied to the [MessageBodyMember] attribute with a value of BookOrderQuantity, which will be the name of the SOAP message XML element.

Order

The Order property of the [MessageBodyMember] attribute specifies the ordinal position in which each message body member will be serialized into the SOAP message body:

[MessageContract]
public class BookOrder
{
[MessageHeader]
public string ISBN
[MessageBodyMember(Order=1)]
public int Quantity
[MessageBodyMember(Order=2)]
public string FirstName
[MessageBodyMember(Order=3)]
public string LastName

In this example, the Order attribute is applied to all the [MessageBodyMember] attributes in the message contract, thus the XML SOAP message will contain the XML elements in the specified order. If no Order parameter is specified, the elements are ordered alphabetically.

Tagged . Bookmark the permalink.

Leave a Reply