In a previous article, I explained about WCF and the need of WCF Service. Also, I explained some fundamentals of WCF services, such as Endpoints, Soap, Message etc. Now, I would like to explain about Contract, which is a vital part of WCF Services.
Contract is a formal agreement between client and service, which defines the way of describing what the service will do for the respective client. Contract describes what service provides. The functionality and operation, message, and the data that needs to be exposed by client, take place inside the contact. So, contract is basically an interface that has some methods to be exposed by the client.
There are four types of contracts available In WCF. Each of them has different properties or purposes.
- Service Contract
- Data Contract
- Message Contract
- Fault Contract
I will explain only two Contracts in this blog.
Service Contract
Service contract is basically an interface that describes the operations or functionality of what a service does. It contains all the methods that would be used by the client.
Please have a look on the given screenshot.

I am creating a WCF Service for students, which has four methods. I have added a [ServiceContract] attribute just before the IStudentWcfService interface. When client adds the reference of service or uses svc.utill utility command for generating the metadata or proxy, then this attribute functions and does an awesome job of taking the responsibility for creating the metadata of service, which would be available to the client.
If we want some internal operations in the service and don’t want to let client access those methods, then we do not decorate with [operationContract] Attribute. Basically, the operationContract provides abstraction over Service or metadata. Only metadata of those methods will be generated which are decorated with [OperationContract] attribute.
In the given example, RemoveStudent() method will not be available to the client. So, the client cannot access this method but he can access the other three.
Data Contract
WCF service uses data contract serializer to serialize the data from object to XML and de-serialize from XML to object. There is a default serializer in WCF that implicitly serializes and de-serializes the data but it works on whole object. Sometimes, we need more control over it and want to include and exclude some members from the serialization or de-serialization process.
The .NET framework primitive types such as integer, float, string and all other types which are considered as primitive, can be serialized automatically or implicitly. We have no need to explicitly serialize these types of data.
We can opt out some members from serialization process by decorating the public field and property with [IgnoreDataMember] attribute. We can do the same with [DataContract] and [DataMember] attribute, if we want to opt out some members, then there is no need to decorate these members with [DataMember] attribute.
There are three public properties in Student class - studentName, studentId and studentFees.

If I want to explain in one single line, “Data Contract is a serialization technique that serializes the data and as well achieves the abstraction by giving the [DataContract] attribute on class and by decorating the public field and property with [DataMember] attribute.”
“Data Contract is a formal agreement between client and service that abstractly describes the data to be exchange."
Note I will explain the rest of two contracts in my next blog. Also, I will upload the code of creating the WCF Service and consuming by MVC client.

Join the conversation! Your thoughts help the community grow.