How to Implement Message Contract in WCF

What is Message Contract?

 
Before discussion about message contract, I want to explain data contracts, messages and envelopes. In WCF a data contract enables us to define the structure of the data. This data is sent in the body of an envelope.
 
Message Contract in WCF 
 
A message is nothing but a packet and WCF uses this packet to transfer the information from source to destination. This message is contained in the header or body.
 
A message contract is used to control the structure of a message body and serialization process. It is used to send/access the information in the soap header. By use of a Message Contract we can customize the parameters sent using a SOAP message between the client and the server. The SOAP header is implemented in the namespace system.web.services.protocol.
  1. [MessageContract]  
  2. public class AutherRequest  
  3. {  
  4.     public string AutherId;  
  5. }  
Now you want to know: What are MessageHeader and MessageBodyMember?
 
Message Header
 
A Message Header is applied to a member of a message contract to declare the member within the message header.
  1. [MessageContract]  
  2. public class AutherRequest  
  3. {  
  4.     [MessageHeader]  
  5.     public string AutherId;  
  6. }  
Message Body Member
 
A Message Body Member is applied to the member of a message contract to declare the members within the message body.
  1. [MessageContract]  
  2. public class AuthorResponse  
  3. {  
  4.     [MessageBodyMember]  
  5.     public Auther Obj;  
  6. }  
Confuse when you go for Message Contract and when for Data Contract
 
Data contracts are used to define the data structure and generate the XML for the data you want to pass. If you want to go for more control on your SOAP Message then you should go for a Message Contract. Do not mix message contract and data contract.
 

Implement Message Contract in WCF 

 
So, now we all are good to go and implement this concept practically.
 
The complete code of Service1.cs looks like this:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8.   
  9. namespace MessageContract  
  10. {  
  11.     [ServiceContract]  
  12.     public interface IService1  
  13.     {  
  14.         [OperationContract]  
  15.         [FaultContract(typeof(string))]  
  16.         AuthorResponse GetInfo(AutherRequest Req);          
  17.     }  
  18.     [MessageContract]  
  19.     public class AutherRequest  
  20.     {  
  21.         [MessageHeader]  
  22.         public string AutherId;  
  23.     }  
  24.     [MessageContract]  
  25.     public class AuthorResponse  
  26.     {  
  27.         [MessageBodyMember]  
  28.         public Auther Obj;  
  29.     }  
  30.    [DataContract]  
  31.     public class Auther  
  32.     {  
  33.         [DataMember]  
  34.         public string FirstName { getset;}  
  35.         [DataMember]  
  36.         public string LastName { getset; }  
  37.         [DataMember]  
  38.         public string Artical { getset; }  
  39.     }  
  40. }  
The complete code of Service1.scv.cs looks like this: 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. namespace MessageContract  
  9. {  
  10.     public class Service1 : IService1  
  11.     {  
  12.         private const string AutherId = "101";  
  13.         public AuthorResponse GetInfo(AutherRequest Req)  
  14.         {  
  15.             if (Req.AutherId != AutherId)  
  16.             {  
  17.                 string x = "Invalid User ID";  
  18.                 throw new FaultException<string>(x);  
  19.             }  
  20.             AuthorResponse r = new AuthorResponse();  
  21.             // create an object of Auther Class  
  22.             r.Obj = new Auther();          
  23.             r.Obj.FirstName = "Amit";  
  24.             r.Obj.LastName = "Tiwari";  
  25.             r.Obj.Artical = "Learn WCF";  
  26.             //r.S.Artical = "Learn WCF";  
  27.             return r;  
  28.         }  
  29.     }  
  30. }  
Execute the code and enter the Author Id to get the author information.
 
MessageWCF2.jpg 
 
MessageWCF3.jpg 
 
Now we need to create a client interface to access this service.
 
MessageWCF4.jpg 
 
Add the service reference into your client application.
 
MessageWCF5.jpg 
 
After adding the service reference, create an object of Service Client Proxy class and call the GetInfo method and display only the first name in the TextBox. If you want to display all the three values in a text box then you need to create two more TextBoxes for that.
 
The complete code of WebForm.aspx.cs looks like this:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.ServiceModel;  
  8. namespace MessageContractClient  
  9. {  
  10.     public partial class WebForm : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.         }  
  15.         protected void Button1_Click(object sender, EventArgs e)  
  16.         {  
  17.             ServiceReference1.Service1Client s = new ServiceReference1.Service1Client("BasicHttpBinding_IService1");  
  18.             ServiceReference1.Auther au=s.GetInfo(TextBox1.Text);  
  19.             TextBox1.Text = au.FirstName;  
  20.         }  
  21.     }  
  22. }  
The complete code of Web.config looks like this: 
  1. <?xml version="1.0"?>  
  2. <!--  
  3.   For more information on how to configure your ASP.NET application, please visit  
  4.   http://go.microsoft.com/fwlink/?LinkId=169433  
  5.   -->  
  6. <configuration>  
  7.     <system.web>  
  8.         <compilation debug="true" targetFramework="4.0" />  
  9.     </system.web>  
  10.     <system.serviceModel>  
  11.         <bindings>  
  12.             <basicHttpBinding>  
  13.                 <binding name="BasicHttpBinding_IService1" />  
  14.             </basicHttpBinding>  
  15.         </bindings>  
  16.         <client>  
  17.             <endpoint address="http://localhost:2316/Service1.svc" binding="basicHttpBinding"  
  18.                 bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"  
  19.                 name="BasicHttpBinding_IService1" />  
  20.         </client>  
  21.     </system.serviceModel>  
  22. </configuration> 


Similar Articles