[MessageContract] Attribute in WCF

Message contracts are defined by applying the [MessageContract] attribute. You then specify specifics for each message using the [MessageBodyMember] and [MessageHeader] attributes. The following example illustrates these three attributes by defining a simple message contract:

[MessageContract]
public class BuyStock
{
    [MessageHeader]
    Public string Title;
    [MessageBodyMember]
    Public decimal Cost;
}

The [MessageContract] attribute defines a strongly typed class that is associated with a SOAP message. This in turn creates a typed message, which then enables the passing of messages between services (service operations). It also provides the ability to use custom messages as parameters within these same services (and service operations).
The following sections discuss the available parameters that can be used with the [MessageContract] attribute.

HasProtectionLevel

The HasProtectionLevel parameter specifies the message protection level. A message can be encrypted, signed, or both. The available values for this property are true and false. If the value is set to true, the message must be either encrypted, signed, or both. A value of false means that no protection level is needed. The default value is false.
The following example indicates that the message has a protection level:

[MessageContract(HasProtectionLevel=true)]
public class BookOrder
{
    ...
}

IsWrapped

A message can be formatted in several ways. One of those ways is the document/wrapper form. In this XML document form, the outer XML element is called the wrapper element.
The IsWrapped parameter is a Boolean value that specifies that the message is in the document/wrapper form and that the XML document contains a wrapper element:

[MessageContract(IsWrapped = True)]
public class BookOrder
{
    ...
}

PretectionLevel

The ProtectionLevel property specifies the protection level of the message. The message can be encrypted, signed, or both.
The value of this parameter comes from the System.Net.SecurityLevel.ProtectionLevel enumeration. The following enumeration values are available:

  • EncryptAndSign: Encrypt and sign data to ensure the confidentiality and integrity of transmitted data.
  • None: Authentication only.
  • Sign: Sign data to help ensure the integrity of the transmitted data, but do not encrypt.

The following example sets the protection level of the service contract to that of None, meaning that only simple authentication is required:

[MessageContract(ProtectionLevel = System.Net.Security.ProtectionLevel.None)]
public interface IBookOrder
{
    ...
}

WrapperName

The WrapperName parameter works in conjunction with the IsWrapped parameter, in that if the IsWrapped parameter is True, this parameter specifies the name of the outer XML element. The following example illustrates how to use the WrapperName parameter:

[MessageContract(IsWrapped=true, WrapperName=”Order”)]
public class BookOrder
{
    ...
}

WrapperNamespace

The WrapperNamespace parameter also works in tangent with the IsWrapped parameter. This parameter allows you to set the namespace of the wrapper element. The following example uses the WrapperNamespace of the XML element on the associated message contract:

[MessageContract(IsWrapped=true, WrapperNamespace=”http://www.PlanetofCoders.com”, WrapperName=”Order”)]
public class BookOrder
{
    ...
}
Tagged . Bookmark the permalink.

Leave a Reply