MessageContract in WCF: Part 7

Before reading this article, I highly recommend reading my previous parts:

A MessageContract provides the full control over Soap messages. Like if you want to send some information in a Soap header then you can send it using a MessageContract. In a MessageContract you can pass the sensitive data, like user credentials, licence key and so on. You can customize the wrappername as well as remove it. In a previous part of this series we learn about DataContracts and how to control Soap messages. But in a DataControl we have limited control.

Create a WCF application and implement the following datacontract.

  1. using System.Runtime.Serialization;  
  2. using System.ServiceModel;  
  3.   
  4. namespace MessageContractImplimentation  
  5. {  
  6.     [ServiceContract]  
  7.     public interface IService1  
  8.     {  
  9.         [OperationContract]  
  10.         string GetData(int value);  
  11.   
  12.         [OperationContract]  
  13.         CompositeType GetDataUsingDataContract(CompositeType composite);  
  14.   
  15.     }  
  16.       
  17.     [MessageContract(IsWrapped=true,WrapperName="TestMessage",ProtectionLevel=System.Net.Security.ProtectionLevel.None,WrapperNamespace="http://localhost:8080/MessageContract")]  
  18.     public class CompositeType  
  19.     {  
  20.         [MessageHeader(Actor="Pramod",MustUnderstand=false,Name="Thakur",ProtectionLevel=System.Net.Security.ProtectionLevel.None,Relay=true)]  
  21.         public string Key { getset; }  
  22.   
  23.         [MessageHeader(Actor="Prem",MustUnderstand=true,Name="Kumar",ProtectionLevel=System.Net.Security.ProtectionLevel.None,Relay=false)]  
  24.         public string PK { getset; }  
  25.   
  26.         [MessageBodyMember(Name="Active",Order=1,ProtectionLevel=System.Net.Security.ProtectionLevel.None)]  
  27.         public bool Status { getset; }  
  28.   
  29.         [MessageBodyMember]  
  30.         public string Data { getset; }  
  31.     }  

Properties of MessageContract

  1. IsWrapped: This property specifies whether the message body has a wrapper element.
    1. [MessageContract(IsWrapped=true)]
  2. WrapperName: Using this property you can get or set the wrappername.
    1. [MessageContract(WrapperName="TestMessage")] 
  3. ProtectionLevel: Using this property you can specify the protection level of the message. The ProtectionLevel is an enum. You can set it either to EncryptAndSign, Sign or None.
    1. [MessageContract(ProtectionLevel=System.Net.Security.ProtectionLevel.None)] 
  4. WrapperNamespace: You can specify the namespace.
    1. [MessageContract(WrapperNamespace="http://localhost:8080/MessageContract")] 

Attributes of MessageContract

  1. MessageHeader: In this attribute we can send the sensitive data like user credentials, Key and so on.
    1. [MessageHeader(Actor="Pramod",MustUnderstand=false,Name="Thakur",ProtectionLevel=System.Net.Security.ProtectionLevel.None,Relay=true)] 
  2. MessageHeaderArray: Specifies that the default wrapper element in the SOAP message must not be written around array types in a header element.
    1. [MessageHeaderArray(Actor="Anuj",MustUnderstand=false,Name="AnujHeaderArray",ProtectionLevel=System.Net.Security.ProtectionLevel.None,Relay=true)] 
  3. MessageBodyMember: By using this we can send the any data.
    1. [MessageBodyMember(Name="Active",Order=1,ProtectionLevel=System.Net.Security.ProtectionLevel.None)] 
  4. MessageParameter: On the MSDN, MessageParameter controls the name of the request and response parameter names. Cannot be used with Message or message contracts.
    1. [MessageParameter(Name="MP")] 
  5. MessageProperty: According to the MSDN, MessageProperty represents data that is passed locally with a custom message type but not serialized into a SOAP message.
    1. [MessageProperty(Name="Prop")] 

I will explain these attributes and properties in detail in a later article.

I hope you will enjoy this article.

Happy coding.

Up Next
    Ebook Download
    View all
    Learn
    View all