How to Implement Message Contracts

Rama Sagar
Posted by in WCF category on for Beginner level | Points: 250 | Views : 10888 red flag
Rating: 4.33 out of 5  
 3 vote(s)

This Article explains you to how to use Message Contracts in Windows communication Foundation...

 Download source code for How to Implement Message Contracts


This Article explains you to how to use Message Contracts in Windows communication Foundation...

Message Contract…..?

• Message Contract are useful when you need a control on the layout of your SOAP Message by adding specific headers and footers to a message...
• They describe the flow of SOAP Messages sent to and from a service and also enables you to Inspect and control all the details in the SOAP Header and body...
• Though WCF automatically manages the message. On some critical issue, we also require control over the SOAP message format. In that case WCF provides Message Contract to customize the message as per requirement.
• Message contract can be applied to type using Message Contract attribute. Custom Header and Body can be included to message using 'Message Header' and 'MessageBodyMember'atttribute as shown below

How they Differ from Data Contracts…..?

Data contracts are used to define the data structure. Messages that are simply a .NET type, let’s say in form of POCO (plain old CLR object), and generate the XML for the data you want to pass. Whereas
Message contracts are preferred when we need to “control” the layout of your message (the SOAP message); by adding specific headers/footer/etc. to a message.


There are some certain rules to be noticed while applying the Message Contract
1. Only one parameter can  be used in service Operation while using message contract type as a parameter
2. Service Operation will accept only message contract as return doesn’t accept any other data types

Let’s look into the sample Implementation:-

Employee Data Contract:-

Add new Employee Data Contract to handle the Employee related info by adding new class to Service library. Add below code to the class and reference to System.Runtime.Serialization


    public class Employee


       // [DataMember (EmitDefaultValue=false,Order=1,Name="Name",IsRequired=true)]


        public string FirstName { get; set;}

     //   [DataMember(EmitDefaultValue = true, Order=0,IsRequired=false)]


        public string LastName { get; set; }

       // [DataMember(EmitDefaultValue = false,Order=2)]

       //   [IgnoreDataMember]


        public string Designation{ get; set; }

Request Message Contract:-

Create a new Message Contract for request by adding new class to Service library. Name it as Employee Request. Add reference to System.ServiceModel


    public class EmployeeRequest



string EmployeeId;


// [IgnoreDataMember]

Response Message Contract:-

Now Create a Message Contract for response.


    public class EmployeeResponse



        public Employee eobj;


Adding Service Contract:-

Add a service contract to handle customer message request and response.

public interface IService1




        EmployeeResponse GetInfo(EmployeeRequest Req);        


Implementation of I Service:-

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

namespace Sample_MessageContract


    // NOTE:You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.

    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.

    public class Service1 : IService1


        private const string EmployeeId = "533";

        public EmployeeResponse GetInfo(EmployeeRequest Req)


            if (Req.EmployeeId != EmployeeId)


                string v = "Invalid User ID";

                throw new FaultException<string>(v);


            EmployeeResponse ER = new EmployeeResponse();

            ER.eobj = new Employee();

            ER.eobj.FirstName = "Rama";

            ER.eobj.LastName = "Sagar";

            ER.eobj.Designation = "Software Developer";

            return ER;




Now Host the Service and invoke the service as shown below

Now take a Sample client application to test the Message Contract  Add an aspx page, create a button and text box. And then add a Service Reference…The end point is created automatically as shown below


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

bindingConfiguration="BasicHttpBinding_IService1" contract="Sample_MessageContract_Referance.IService1" name="BasicHttpBinding_IService1" />


Now run the Application and check with the employee id


In this article, we examined how to implement Message Contracts .and what are the advantages of using them…..You can download the complete source code and can test it………

Thanks for having a look at my article………

Page copy protected against web site content infringement by Copyscape

About the Author

Rama Sagar
Full Name: RamaSagar Pulidindi
Member Level: Silver
Member Status: Member,MVP
Member Since: 12/30/2012 1:51:40 AM
Country: India
A Software Profesional working in Microsoft .NET technologies since year 2008, and I work for Dake ACE. I am passionate about .NET technology and love to contribute to the .NET community at Dot Net Funda

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)