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. namespace MessageContractImplimentation
  4. {
  5. [ServiceContract]
  6. public interface IService1
  7. {
  8. [OperationContract]
  9. string GetData(int value);
  10. [OperationContract]
  11. CompositeType GetDataUsingDataContract(CompositeType composite);
  12. }
  13. [MessageContract(IsWrapped=true,WrapperName="TestMessage",ProtectionLevel=System.Net.Security.ProtectionLevel.None,WrapperNamespace="http://localhost:8080/MessageContract")]
  14. public class CompositeType
  15. {
  16. [MessageHeader(Actor="Pramod",MustUnderstand=false,Name="Thakur",ProtectionLevel=System.Net.Security.ProtectionLevel.None,Relay=true)]
  17. public string Key { get; set; }
  18. [MessageHeader(Actor="Prem",MustUnderstand=true,Name="Kumar",ProtectionLevel=System.Net.Security.ProtectionLevel.None,Relay=false)]
  19. public string PK { get; set; }
  20. [MessageBodyMember(Name="Active",Order=1,ProtectionLevel=System.Net.Security.ProtectionLevel.None)]
  21. public bool Status { get; set; }
  22. [MessageBodyMember]
  23. public string Data { get; set; }
  24. }
  25. }
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.
Next >> Tracing in WCF: Part 8