Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Understanding Data Contract in WCF

, 25 Jul 2014
Rate this:
Please Sign up or sign in to vote.
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. I

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

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

License

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

Share

About the Author

Imran Abdul Ghani
Software Developer (Senior)
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.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

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