Before reading this article, I highly recommend reading my previous parts:
- Introduction to WCF: Part 1
- Introduction to Endpoint in WCF: Part 2
- How to Make Changes to WCF Service Without Breaking Client in WCF: Part 3
- How to Make Changes to WCF Service Without Breaking Client in WCF: Part 3.
- Method overloading in WCF – Part 4.
- What a DataContract and DataMember are in WCF: Part 5.
- KnownType Attribute in WCF: Part 6
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; }
- }
- }

Comments
Join the conversation! Your thoughts help the community grow.