Define Message contracts

So far we’ve looked into the ways of controlling the underlying service metadata on the client and the server. This time, we’ll inspect how to customize the message itself, with the help of message contracts.

As you could guess, there are a bunch of attributes which help us implementing this task. It’s good to know your enemy from the beginning, so here are them:

  • MessageContractAttriubte: this attribute let’s you control the mappings between your custom types and their SOAP representations.
  • MessageBodyMemberAttribute: the given field/property will be serialized as a member of the SOAP body.
  • MessageHeaderAttribute: surprise! The given field/property will be serialized into a SOAP header.
  • MessageHeaderArrayAttribute: defines that no wrapper element should be written to the specified array (each value in the array will be an independent element).

Well, only four attributes. That’s not a big deal for us, so let’s dive into the details!

Defining message contracts are easy. Just mark your custom class (or struct) with the MessageContractAttribute.  Luckily, we won’t miss the named parameters, which are as follows:

Named parameters of MessageContractAttribute
IsWrapped A Boolean value indicating whether or not a wrapper is present on the SOAP body.
ProtectionLevel The good old ProtectionLevel, can be EncryptAndSign, Sign or None.
WrapperName The name of the body wrapper.
WrapperNamespace The namespace of the body wrapper.


After you’ve marked your class with MessageContractAttribute, it’s time to create the mappings, which is the whole purpose of our work. MessageBodyMemberAttribute, MessageHeaderAttribute and MessageHeaderArrayAttribute will be much help in implementing this. Consider the following example. After that, more named parameters will come, so stay tuned!

public class MyMail
public string Sender {get; set;}
public string[] Receiver {get; set;}
public string Subject {get; set;}

Our simple mail class will be similar to the following XML:

<Subject>WCF 70-503</Subject>

Now as I promised, the named parameters:

  • MessageBodyMember
    • Name: the name of the element.
    • Namespace: the namespace of the element.
    • ProtectionLevel: the protection level specified.
    • Order: the ordinal number in which the property should be deserialized.
    • MessageHeader/MessageHeaderArray
      • Actor: the URI that indicated the node at which the header is targeted.
      • MustUnderstand: specifies that the actor must understand the given header.
      • Name: the name of the header element.
      • Namespace: the namespace of the header element.
      • ProtectionLevel: the protection level…
      • Relay: a Boolean specifying whether or not this header is to be relayed to downstream nodes.

We’ve stumbled into ProtectionLevel in numerous times. Let’s take a closer look. ProtectionLevel (full name is System.Net.Security.ProtectionLevel enumeration) has three members: EncryptAndSign, Sign and None. The most secure on is EncryptAndSign, which encrypts and signs the message (or part of it). On the SOAP header level, all headers valued individually, so encrypting and signing a header won’t affect a header which is only signed. On the body level, the minimum protection level is enforced, which is the highest level specified. So if you mark a SOAP body element with EncryptAndSign, all other elements will be encrypted and signed, regardless of their settings. I think you can grab the further logic from here.

You can read some quite useful information about the topic on MSDN, on the link below, so be sure to check it out!

Further Readings

Using Message Contracts

Tags: , , ,

One response to “Define Message contracts”

  1. Indu says :

    Do you have any example application where to create a WCF Rest service, host it on IIS, consume the service in WPF client and also data access layer to and stored procedure. I need to display data on grid with all the above

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: