Understanding Data Contract in WCF

By | July 20, 2014

WCF Data Contract

In Windows Communication Foundation, Data Contract is an agreement between parties (i.e. a service and a client) that describes what type of data will be exchanged between them? We can control the contents (i.e. Payload data or message body) of a SOAP message using Data Contract.
In more simple words, we can say that it’s basically the data that is passed to or returned from when a Service Method is called. For example, consider a WCF Service “StudentService” having Service Method named “SaveStudentInfo” that takes StudentInfo object as parameter. In this case, StudentInfo is our Data Contract that is exchanged between WCF Service and client. It can be easily understood by following diagram.

WCF Data Contract

Data Contract defines that how a data type (e.g. StudentInfo) is serialized at one end and later deserialized at other end. Before an object being transmitted over the network, it’s serialized (means object converts to a sequence of bytes) and deserialized (means sequence of bytes converts back to object) at the other end.

As we discussed earlier that WCF Service Contract can be mapped to WSDL (Web Service Description Language), similarly WCF Data Contract can be mapped to XML Schema. Data Contract is a Structural Contract, other contract in this category is Message Contract. Let’s understand Data Contract in WCF by implementing a simple real world example.

To create a Data Contract in WCF, we will create a StudentInfo class and assign a DataContract attribute to it as follows:

[DataContract]
public class StudentInfo
{
             private string sFirstName;
             public string FirstName
             {
                     get { return sFirstName; }
                    set { sFirstName = value; }
             }
             private string sLastName;
             public string LastName
             {
                    get { return sLastName; }
                   set { sLastName = value; }
              }             
              private string registrationNumber;
             public string RegistrationNumber
             {
                  get { return registrationNumber; }
                 set { registrationNumber = value; }
             }
}

Now, the service will have a method named GetStudentInfo that takes studentID as parameter and returns StudentInfo object. When proxy will be generated on client side, it will have information about StudentInfo because StudentInfo is marked with DataContract attribute. For more information about WCF Proxy and how to generate one, please follow here. Implementation of our Service Contract using above given DataContract is as follows:

[ServiceContract]
public interface IStudentService
{
       [OperationContract]
       StudentInfo GetStudentInfo(int studentID);
}
public class StudentService : IStudentService
{
        public StudentInfo GetStudentInfo(int studentID)
       {
             StudentInfo studentInfo = new StudentInfo();
             //Code Here
             //Fetch data and load studentInfo
             return studentInfo
        }
}

On client side, when we consume this WCF Service and call GetStudentInfo method, we will receive StudentInfo object that we can use or render according to our need.

It’s now quite clear that what a WCF Data Contract is? And how to use Data Contracts in our WCF Service.

Other WCF and related Tutorials:

Category: Uncategorized Tags:

About IMRAN ABDUL GHANI

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.