Message Contract Example

Earlier we discuss two contract i.e. service contract and data contract. Now in this article I will discuss message contract.

In message contract data is sent in the format of SOAP message which in the form of header and body. With the help of message header we can implement security by passing credential in header part.

Example

Create wcf service and add class to implement message contract.

[MessageContract]
public class EmployeeRecord
{
    [MessageHeader]
    public string token;

   
    [MessageBodyMember]
    public string name;
  
   [MessageBodyMember]
    public int empid;
}

		

Now create contract and create method in it which takes the EmployeeRecord object as input and return the same

		[ServiceContract]
public interface ITechAltum
{
    [OperationContract]
    EmployeeRecord EmpData(EmployeeRecord er);
   
}

Now implement it in the class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class TechAltum:ITechAltum
{
   public EmployeeRecord EmpData(EmployeeRecord er)
    {

        if (er.token == "1234")
        {
            er.empid = 101;
            er.name = "Isha";
       
        }
        return er;
    }

}

		

As you can see that it checks the token, if token is valid then it assign information in the same class.

Now implement it in the client side.

As we know that in the method it takes EmpRecord types as input and it returns it same as output. But at client side we will work on Ref parameter in this case which is shown as follows:-

		message contract in wcf
		
		Figure 1
		

As you can see it shows that this method is void type. So we will pass input as ref type parameter.

		using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ServiceReference1.TechAltumClient svctech = new ServiceReference1.TechAltumClient();
        ServiceReference1.EmployeeRecord empRec = new ServiceReference1.EmployeeRecord();

        empRec.token = "1234";
        //we pass input as ref type
        svctech.EmpData(ref empRec.token, ref empRec.empid, ref empRec.name);

        Response.Write(empRec.empid + "   " + empRec.name);

    }
}

		

Copy this url and create new website where we will call this service. And click on the add service reference.

Similarly if we do not pass any input parameter to the service in that case we pass these data as out parameter.

Add another method in your contract which returns the object of EmployeeRecord and not taking any input:-

		[ServiceContract]
public interface ITechAltum
{
   
    [OperationContract]
    EmployeeRecord EmpOutData();
}

Now implement it in the class:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class TechAltum:ITechAltum
{
      public EmployeeRecord EmpOutData()
   {

       EmployeeRecord er = new EmployeeRecord();
       er.empid = 102;
       er.name = "Priya";
       er.token = "P-123";
       return er;
  
  
   }
}

		

Now see the code at client side:-

		message contract example
		
		Figure 2
		

As you can see that now it’s taking input as out and its return type is showing as string. This is message header which is Token and its data type is string. In this case it will return token.

		using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ServiceReference1.TechAltumClient svctech = new ServiceReference1.TechAltumClient();
        ServiceReference1.EmployeeRecord empRec = new ServiceReference1.EmployeeRecord();

        empRec.token=.svctech.EmpOutData(out empRec.empid, out empRec.name);

        Response.Write(empRec.empid + "     "+ empRec.name+ "        " empRec.token);

    }
}

		

Note:-if you are not aware about the concept of Ref and Out then kindly study it first.

Email Address

For any query you can mail me at Malhotra.isha3388@gmail.com.