Anatomy of MessageContractAttribute

Intent

The primary reason to use MessageContractAttribute over DataContractAttribute is the ability to customize SOAP message header.  A fringe benefit is partial encryption.

Noted that this is an approach to specify data contract. For more information, please see Making Sense With Data Contract (Structural Contract).

Minimal Code to Specify Message Contract

 1 namespace MessageContractLibrary
 2 {
 3   using System.ServiceModel;
 4 
 5   /* MessageContractAttribute specifies the type as message contract. */
 6   [MessageContract]
 7   public class MyMessageContract
 8   {
 9     /* MessageHeaderAttribute specifies a member as message header. */
10     [MessageHeader] public int MyHeader;
11 
12     /* MessageHeaderAttribute can also specifies array. */
13     [MessageHeader] public int[] MyHeader2;
14 
15     /* MessageBodyMemberAttribute specifies a member as message header. */
16     [MessageBodyMember] public string MyBody;
17   }
18 }
19 /* The above class defines this SOAP message definition.
20 <s:Envelope ...>
21   <s:Header>
22     <h:MyHeader xmlns:h="http://tempuri.org/">1</h:MyHeader>
23     <h:MyHeader2 xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:h="http://tempuri.org/">
24       <int xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">1</int>
25       <int xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">2</int>
26     </h:MyHeader2>
27     ...
28   </s:Header>
29   <s:Body>
30     <MyMessageContract xmlns="http://tempuri.org/">
31       <MyBody>A</MyBody>
32     </MyMessageContract>
33   </s:Body>
34 </s:Envelope>
35 */

Here’re notes on specifing collection and array.

 1 namespace MessageContractLibrary
 2 {
 3   using System.Runtime.Serialization;
 4   using System.ServiceModel;
 5 
 6   [MessageContract]
 7   public class MyMessageContract2
 8   {
 9     /* MessageHeaderAttribute specifies an array as message header. */
10     [MessageHeader] public int[] MyHeader2;
11 
12     /* MessageHeaderArrayAttribute specifies an array as message header list. */
13     [MessageHeaderArray] public int[] MyArray;
14 
15     /* MessageHeaderAttribute applies to data contract. */
16     [MessageHeader] public MyDataContract[] MyHeaders;
17   }
18 
19   [DataContract]
20   public class MyDataContract
21   {
22     [DataMember] public string MyDataMemeber;
23   }
24 }

Additional Specification Options

Name and Namespace

Use Name and Namespace properties to rename message name and namespace.

 1 namespace MessageContractLibrary
 2 {
 3   using System.ServiceModel;
 4 
 5   [MessageContract]
 6   public class MyNamedMessageContract
 7   {
 8     [MessageHeader(Name = "NamedHeader")] public string MyHeader1;
 9 
10     [MessageHeader(Namespace = "http://test/")] public string MyHeader2;
11 
12     [MessageBodyMember(Name = "NamedBody")] public string MyBody1;
13 
14     [MessageBodyMember(Namespace = "http://test2/")] public string MyBody2;
15   }
16 }
17 /*
18 <s:Envelope ...>
19   <s:Header>
20     <h:NamedHeader xmlns:h="http://tempuri.org/">A</h:NamedHeader>
21     <h:MyHeader2 xmlns:h="http://test/">B</h:MyHeader2>
22     ...
23   </s:Header>
24   <s:Body>
25     <MyNamedMessageContract xmlns="http://tempuri.org/">
26       <NamedBody>C</NamedBody>
27       <MyBody2 xmlns="http://test2/">D</MyBody2>
28     </MyNamedMessageContract>
29   </s:Body>
30 </s:Envelope>
31 */

API Reference

Further Study