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.
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:
public class StudentInfo
{
private string sFirstName;
public string FirstName
{
get { return sFirstName; }
set { sFirstName = value; }
}
public string LastName
{
get { return sLastName; }
set { sLastName = value; }
}
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:
public interface IStudentService
{
[OperationContract]
StudentInfo GetStudentInfo(int studentID);
}
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:
- Top 10 WCF Interview Questions
- WCF Hosting (Console | Windows Service | IIS | WAS )
- 5 simple steps to create your first WCF RESTful Service
- WCF Vs ASMX Service
- Top 10 Html5 Interview Questions
- MVC3 Vs MVC4 Vs MVC5
- Difference betweeen ASP.NET WebForms and ASP.NET MVC
- 3 ways to generate proxy for WCF Service
- Post JSON data to WCF RESTful Service using jQuery
- Practical Guide to ASP.NET Web API