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.
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:
[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 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:
[ServiceContract]
public interface IStudentService
{
[OperationContract]
StudentInfo GetStudentInfo(int studentID);
}
public class StudentService : IStudentService
{
public StudentInfo GetStudentInfo(int studentID)
{
StudentInfo studentInfo = new 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.