Contract In Windows Communication Foundation (WCF)

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 some different types of contract for different purposes. Let me explain more about contract.
 
Suppose, we are creating a service for students. Then, the functionality of this service would be adding the student, seeing the details of a student, and showing the list of students. So, we need to create a contract for performing these operations which will be used by the clients whenever they need it.

There are four types of contracts available In WCF. Each of them has different properties or purposes.
  1. Service Contract
  2. Data Contract
  3. Message Contract
  4. 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.

The availability of metadata is necessary to the client for communicating to the service. All operations and methods are declared in interface, so we need to have interface reference or interface metadata on the client side, for making a connection to the service. Then, [ServiceContract] attribute helps us creating the metadata.

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.
 
The other thing we are observing is that it has four methods. Only three methods are decorated with [OperationContract] and one is blank. There is a reason for it.

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.
 
But, when we talk about complex data types, such as student object or others,  we need to explicitly decorate with [DataContract] attribute for achieving the abstraction of object or more control on visibility of object members. However, if we do not decorate these data types with [DataContract] attribute, there would not be any problem or issue to the client. But then, it will violate the Encapsulation and Abstraction of OOPS because it will expose all the public variables and properties to the client side.
 
For achieving the abstraction, we must decorate our class with [DataContract] attribute and use [DataMember] attribute to decorate the class member such as variable and properties.

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.
studentName and studentId are decorated with DataMember attribute but studentFees doesn’t have DataMember attribute.
Now, it is clearly described that these two properties which are decorated with [DataMember] will be exposed and accessed by the client while the studentFees property would not be accessible by client.



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.
Ebook Download
View all
Learn
View all