Data Contracts in WCF


In this article we will discuss about the data contracts in wcf.

Data contract defines the data types that are being transmitted to and from the client and service. This contract describes the data types those are exposed by the service.

The exposed data types are as simple as int, string, char and also as complex as user defined data types like class, structure.

Here we will see an example to clear our fundamentals on Data Contracts.

Behind The Scene 

The Data contract defines the how the data types are serialized and deserialized; In serialization we convert an object to an array of bytes and through deserialization we convert the byte array to an object.

The .NET framework provides a namespace named "System.Runtime.Serialization" for the this contract.

A data contract has many data members in side it, these are the individual data types.



Lets create a service that returns a class named Employee; where the Employee class holds all the details of the Employee like Employee ID, Name, Age, Department, Phone No, Email Id.

So, for this requirement our service interface will look like this

    public interface IEmployeeService
        Employee GetEmployeeDetails(int EmpId);

For this let's design the data contract keeping the data types of the attributes we have mentioned above.

    public class Employee
 private int emp_ID;
        private string emp_Name;
        private int emp_Age;
        private String emp_Department;
        private int emp_PhoneNo;
        private string emp_EmailId;

        public int ID
            get { return emp_ID;}
            set { emp_ID = value; }

        public string Name
            get { return emp_Name;}
            set { emp_Name = value; }

        public int Age
            get { return emp_Age; }
            set { emp_Age = value; }

        public int DepartMent
            get { return emp_Department; }
            set { emp_Department = value; }

        public int PhoneNo
            get { return emp_PhoneNo; }
            set { emp_PhoneNo = value; }

        public string EmailId
            get { return emp_EmailId; }
            set { emp_EmailId = value; }


In the above contract we have defined the data contract containing the data members those will be transmitted over the service - client comunication.

Here is the implementation of the service class;
public class EmployeeService : IEmployeeService
        public Employee GetEmployeeDetails(int empId)
            Employee empDetail = new Employee();

            //Do something here 

            return empDetail;

This method inside the service will return the user defined class ( Employee ) that holds many details of the Employee like name, id age etc.

Now, see the below method how to use this in client side programing.

protected void ShowEmplyeeDetails()
     EmployeeServiceClient objEmployee = new EmployeeServiceClient();
     Employee employee;
     employee = objEmployee.GetEmployeeDetails( employeeID );
     // Access the attributes of the Employee class
     lblEmpID.Text = employee.ID;
     lblEmpName.Text = employee.Name;
     lblEmpAge.Text = employee.Age;
     lblEmpDepartment.Text = employee.DepartMent;
     lblEmpPhoneNo.Text = employee.PhoneNo;
     lblEmpEmailId.Text = employee.EmailId;

In client side once the service returns you the object then you would be able to access the data inside that object like above method.

Hope, this article gives you some idea about the Data Contracts in WCF.

Happy Coding...

Previous Next

No comments:

Post a Comment