How to Implement 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.

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.

MessageWCF1.jpg

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.

    [MessageContract]

    public class AutherRequest

    {

        public string AutherId;

 

    }

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.

 

    [MessageContract]

    public class AutherRequest

    {

        [MessageHeader]

        public string AutherId;

 

    }

Message Body Member

A Message Body Member is applied to the member of a message contract to declare the members within the message body.
 

    [MessageContract]

    public class AuthorResponse

    {

        [MessageBodyMember]

        public Auther Obj;

 

    }

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.

So, now we all are good to go and implement this concept practically.

The Complete Code of Service1.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MessageContract

{

    [ServiceContract]

    public interface IService1

    {

        [OperationContract]

        [FaultContract(typeof(string))]

        AuthorResponse GetInfo(AutherRequest Req);        

    }

 

    [MessageContract]

    public class AutherRequest

    {

        [MessageHeader]

        public string AutherId;

 

    }

 

 

    [MessageContract]

    public class AuthorResponse

    {

        [MessageBodyMember]

        public Auther Obj;

 

    }

 

   [DataContract]

    public class Auther

    {

        [DataMember]

        public string FirstName { get; set;}

 

        [DataMember]

        public string LastName { get; set; }

    

        [DataMember]

        public string Artical { get; set; }

 

 

    }  

}


The Complete Code of Service1.scv.cs looks like this:
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

 

namespace MessageContract

{

    public class Service1 : IService1

    {

 

        private const string AutherId = "101";

 

        public AuthorResponse GetInfo(AutherRequest Req)

        {

 

            if (Req.AutherId != AutherId)

            {

                string x = "Invalid User ID";

                throw new FaultException<string>(x);

            }

                                

           AuthorResponse r = new AuthorResponse();

// create an object of Auther Class

           r.Obj = new Auther();        

           r.Obj.FirstName = "Amit";

           r.Obj.LastName = "Tiwari";

           r.Obj.Artical = "Learn WCF";

            //r.S.Artical = "Learn WCF";

            return r;

        }

 

      

    }

}


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:
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ServiceModel;

 

 

namespace MessageContractClient

{

    public partial class WebForm : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

 

ServiceReference1.Service1Client s = new ServiceReference1.Service1Client("BasicHttpBinding_IService1");

            ServiceReference1.Auther au=s.GetInfo(TextBox1.Text);

            TextBox1.Text = au.FirstName;          

 

        }

    }

}

The Complete Code of Web.config looks like this:
 

<?xml version="1.0"?>

 

<!--

  For more information on how to configure your ASP.NET application, please visit

  http://go.microsoft.com/fwlink/?LinkId=169433

  -->

 

<configuration>

    <system.web>

        <compilation debug="true" targetFramework="4.0" />

    </system.web>

 

    <system.serviceModel>

        <bindings>

            <basicHttpBinding>

                <binding name="BasicHttpBinding_IService1" />

            </basicHttpBinding>

        </bindings>

        <client>

            <endpoint address="http://localhost:2316/Service1.svc" binding="basicHttpBinding"

                bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"

                name="BasicHttpBinding_IService1" />

        </client>

    </system.serviceModel>

</configuration>