Click here to Skip to main content
13,342,442 members (54,086 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 25 Jul 2014

Understanding Data Contract in WCF

, 25 Jul 2014
Rate this:
Please Sign up or sign in to vote.
Understanding data contract in WCF

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 the following diagram.

WCF Data Contract

Data Contract defines how a data type (e.g. StudentInfo) is serialized at one end and later deserialized at the other end. Before an object is 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, 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:

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 the 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:

public interface IStudentService
    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 the 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 what a WCF Data Contract is? And how to use Data Contracts in our WCF Service.

Other WCF and Related Tutorials

The post Understanding Data Contract in WCF appeared first on WCF Tutorial.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Imran Abdul Ghani
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 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 WCF Tutorials, Web Development, SharePoint for Dummies.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 25 Jul 2014
Article Copyright 2014 by Imran Abdul Ghani
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid