MessageContract in WCF – Part 7

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

MessageContract provide the full control over the soap messages. Like if i want to send some information in soap header then we can send it using messsagecontract. We

can customise the wrappername as well as remove it. In previous part of this series we learn about DataContract and leant that how can we control over the soap

message.But in DataControl we have limited control

Create a wcf application and implement the following datacontract.

using System.Runtime.Serialization; using System.ServiceModel; namespace MessageContractImplimentation { [ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); } [MessageContract(IsWrapped=true, WrapperName = "TestMessage", ProtectionLevel = System.Net.Security.ProtectionLevel.None , WrapperNamespace = "http://localhost:8080/MessageContract")] public class CompositeType { [MessageHeader (Actor = "Pramod" , MustUnderstand = false,Name = "Thakur" , ProtectionLevel=System.Net.Security.ProtectionLevel.None, Relay = true)] public int Key {get; set;} [MessageHeader (Name="Active" , Order=1 , ProtectionLevel=System.Net.Security.ProtectionLevel.None)] public bool Status {get; set;} } }

Properties of MessageContract

1. IsWrapped: This property specify that the message body has wrapper element or not.


2. WrapperName: By using this property you can get or set the wrappername.


3. ProtectionLevel: By using this property you can specify the protection level of message. ProtectionLevel is an enum. You can set it as EncryptAndSign, Sign or None.


4. WrapperNamespace: You can specify the namespace.


Attributes of MessageContract

1. MessageHeader: In this attribute we can send the sensitive data like user credentials, Key etc.

[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.

[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.


4. MessageParameter:According to MSDN, MessageParameter controls the name of the request and response parameter names. Cannot be used with Message or message contracts.


5. MessageProperty:According to MSDN, MessageProperty represents data that is passed locally with a custom message type but not serialized into a SOAP message.


I will explain these attribute and property in detail in later article.

I hope you will enjoy this article.

Happy coding 🙂

Tags: , , ,