[MessageHeader] Attribute in WCF

The [MessageHeader] attribute maps a SOAP message header to fields and properties of a type marked with the [MessageHeader] attribute. The fields or properties can be a simple or composite type that can be serialized:

[MessageContract]
public class BuyBook
{
    [MessageHeader]
    Public string Title;
}

The following sections list the available parameters used with the [MessageHeader] attribute. As a note, Windows Communication Foundation does not process any of the following attribute properties on incoming messages except for the MustUnderstand property. You can, however, read and write these attributes.

Actor

As messages are sent from sender to receiver, WCF provides a mechanism to specify that a specific message is targeted to a specific endpoint. The Actor parameter provides this functionality in that it allows you to specify the node in which the message header is targeted:

[MessageContract]
public class BuyBook
{
    [MessageHeader(Actor=”http:www.PlanetofCoders.com/WCF”)]
    Public string Title;
}

The version of SOAP being used determines the attribute mapping. In SOAP 1.1, the attribute is mapped to the actor header, whereas in SOAP 1.2 this is mapped to the role header attribute.

MustUnderstand

The MustUnderstand parameter is used in conjunction with the Actor parameter. The MustUnderstand parameter is a true/false parameter. If this parameter is set to True, the URI specified in the Actor parameter must understand the associated incoming header in order to appropriately process the header.
The following example shows the Actor and MustUnderstand parameters in use:

[MessageContract]
public class BuyBook
{
    [MessageHeader(Actor="http://www.PlanetofCoders.com/WCF",MustUnderstand="true")]
    Public string Title;
}

It should be noted that a fault will be generated if the MustUnderstand property is set to true and the receiving application does not understand the header. WCF does not process any message header attributes on incoming messages other than the MustUnderstand attribute.

Name

The Name parameter specifies the name of the message contract element. The value specified must be a valid XML element name:

[MessageContract]
public class BuyBook
{
    [MessageHeader(Name=”Title”)]
}

Namespace

The Namespace parameter specifies the namespace of the message contract element:

[MessageContract]
public class BuyBook
{
    [MessageHeader(Namespace=”http://www.PlanetofCoders.com/WCF/Title”)]
}

Relay

The Relay parameter tells the endpoint that it is processing the message to pass, or relay, the message to the next endpoint specified in the Actor parameter once the current endpoint is finished processing the message:

[MessageContract]
public class BuyBook
{
    [MessageHeader(Actor=”http://www.PlanetofCoders.com/WCF1” Relay=”true”)]
    Public string Title;
    [MessageHeader(Actor=”http://www.PlanetofCoders.com/WCF2” Relay=”false”)]
    Public string Title;
}
Tagged . Bookmark the permalink.

Leave a Reply