WCF Contracts, Data Contract in WCF

 

A data contract is a formal agreement between the WCF service and its client which describes the data to be exchanged. That means for communication between WCF service and its client they no need to share any types, they have to share data contracts.

 

Data contract defines data types which are passes between service and its client. WCF supports CLR data type’s string integer…etc as implicit contracts. Even we can easily create the explicit contracts also.

 

Here we discuss about both implicit and explicit data contracts by creating the WCF service and its client in Visual Studio 2010.  You can do the same thing Visual Studio 2008 also.

 

Create WCF Service by opening visual studio=>New Project=>select WCF Service Application

Design the WCF service contract and Data Contract as shown below.

 

namespace WCFDataContractExp

{

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.

    [ServiceContract]

    public interface IService1

    {

        [OperationContract]

        int GetEmpId(int Id);

 

        [OperationContract]

        string GetEmpName(Name name);

    }

 

    // Use a data contract as illustrated in the sample below to add composite types to service operations.

    [DataContract]

    public class Name

    {

        [DataMember]

        public string FirstName

        {

            get;

            set;

        }

 

        [DataMember]

        public string LastName

        {

            get;

            set;

        }

    }

}
 

 

Here Name is data contract which has two properties FirstName and LastName. You have to apply DataContract attribute to make class as Data contract. Within the Data contract you have to apply DataMember attribute to make available any property for the client. If you are not applying DataMember attribute that property not available for the client.

 

We are using this DataContract in the service contract along with other implicit contracts. GetEmpName() method takes Name as explicit data contract and returns string as implicit data contract. Implement this service contract Iservice1 as shown below.

 

public class Service1 : IService1

{

        #region IService1 Members

 

        public int GetEmpId(int Id)

        {

            return Id;

        }

 

        public string GetEmpName(Name name)

        {

            return name.FirstName + " " + name.LastName;

        }

 

        #endregion

}

 

As shown above GetEmpName() method returns the employee full name by adding first name and last name.

 

Create WCF client to invoke above WCF service. To create Console Application as WCF client open visual studio=>New Project and select Console Application.

 

Then right click on solution explorer and select Add Service Reference and provide the url of the service. Call the WCF service from client as shown below.

 

class Program

{

        static void Main(string[] args)

        {

            ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();

            ServiceReference1.Name objName = new ServiceReference1.Name();

            objName.FirstName = "Kiran";

            objName.LastName = "Kumar";

 

            Console.WriteLine("Employee Id: " + obj.GetEmpId(1).ToString());

            Console.WriteLine("Employee Name: " + obj.GetEmpName(objName));

 

            Console.ReadLine();

        }

}

 

Pass employee id, DataContract properties FirstName and LastName. Then call GetEmpId() and GetEmpName() methods. The output is as shown below.

 

   

                                                                                                    WCFDataContractExp.zip (19.62 kb)

                                                                                                 WCFDataContractClient.zip (48.73 kb)