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.
- 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 string Key { get; set; }
-
- [MessageHeader(Actor="Prem",MustUnderstand=true,Name="Kumar",ProtectionLevel=System.Net.Security.ProtectionLevel.None,Relay=false)]
- public string PK { get; set; }
-
- [MessageBodyMember(Name="Active",Order=1,ProtectionLevel=System.Net.Security.ProtectionLevel.None)]
- public bool Status { get; set; }
-
- [MessageBodyMember]
- public string Data { get; set; }
- }
- }
Properties of MessageContract
- IsWrapped: This property specifies whether the message body has a wrapper element.
- [MessageContract(IsWrapped=true)]
- WrapperName: Using this property you can get or set the wrappername.
- [MessageContract(WrapperName="TestMessage")]
- 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.
- [MessageContract(ProtectionLevel=System.Net.Security.ProtectionLevel.None)]
- WrapperNamespace: You can specify the namespace.
- [MessageContract(WrapperNamespace="http://localhost:8080/MessageContract")]
Attributes of MessageContract
- MessageHeader: In this attribute we can send the sensitive data like user credentials, Key and so on.
- [MessageHeader(Actor="Pramod",MustUnderstand=false,Name="Thakur",ProtectionLevel=System.Net.Security.ProtectionLevel.None,Relay=true)]
- 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)]
- MessageBodyMember: By using this we can send the any data.
- [MessageBodyMember(Name="Active",Order=1,ProtectionLevel=System.Net.Security.ProtectionLevel.None)]
- MessageParameter: On the MSDN, MessageParameter controls the name of the request and response parameter names. Cannot be used with Message or message contracts.
- [MessageParameter(Name="MP")]
- MessageProperty: According to the MSDN, MessageProperty represents data that is passed locally with a custom message type but not serialized into a SOAP message.
- [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.