WCF (2), Contracts In Various Types

In this series of articles, we will discuss the major features of WCF:
ABC of an EndPoint in WCF,
Sample Image - maximum width is 600 pixels
Contracts are in the core position of WCF endpoints. This article will discuss more details of WCF Contracts.


A Contract is an agreement between the two parties, WCF contract is between a Service and a Client. In WCF, Contracts are categorized as behavioral or structural.
  • Behavioral Contract

    • ServiceContract attribute is used to mark a type as Service Contract that contains operations.
    • OperationContract attribute is used to mark the operations that will be exposed.
    • FaultContract defines what errors are raised by the service being exposed.
  • Structual Contract

    • DataContract attribute defines types that will be moved between the parties.
    • MessageContract attribute define the structure of the SOAP message.

Behavioral Contract

A service contract defines the operations which are exposed by the service to the outside world. A service contract is typically an interface of the WCF service and it tells the outside world what the service can do. 
  1. [ServiceContract]    
  2. public interface IService1    
  3. {    
  5.     // TODO: Add your service operations here    
An operation contract is defined within a service contract. It defines the parameters and return type of an operation. An operation contract can also defines operation-level settings, like as the transaction flow of the operation, the directions of the operation (one-way, two-way, or both ways), and fault contract of the operation.
  1. [ServiceContract]
  2. public interface IService1    
  3. {    
  4.     [OperationContract]    
  5.     string GetData(int value);    
  7.     [OperationContract]    
  8.     CompositeType GetDataUsingDataContract(CompositeType composite);    
  10. }   
A Fault Contract is a way to handle an error/exception in WCF. In C# we can handle the error using try and catch blocks at the client side. The purpose of a Fault Contract is to handle an error by the service class and display in the client side.
  1. [ServiceContract]    
  2. public interface IGetDetailsService    
  3. {    
  4.      [OperationContract]    
  5.      [FaultContract(typeof(Student))]    
  6.      Student GetDetails(string Name);    
  7. }  

Structual Contract

The DataContract is a totally different beast --- it define the data type for variables that are the same as get and set properties but the difference is that a Data Contract in WCF is used to serialize and deserialize the complex data. It defines how data types are serialized and deserialized. Using serialization, you can convert an object into a sequence of bytes that can be transmitted over a network. Using de-serialization, you reassemble an object from a sequence of bytes that you receive from a calling application.
The DataMemberAttribute must then be applied to each member of the data contract type to indicate that it is a data member, that is, it should be serialized.
  1. [DataContract]    
  2. public class Student    
  3. {    
  4.      private string _Name;      
  5.      private string _City;    
  7.      [DataMember]    
  8.      public string Name    
  9.      {    
  10.          get { return _Name; }    
  11.          set { _Name = value; }    
  12.      }    
  14.      [DataMember]    
  15.      public string City    
  16.      {    
  17.          get { return _City; }    
  18.          set { _City = value; }    
  19.      }    
A MessageContract is an abstraction over a SOAP message that allows you to explicitly dictate the structure of the underlying message. 
The default SOAP message format is provided by the WCF runtime for communication between the client and the service. If it does not meet your requirements then we can create our own message format. This can be done using the Message Contract attribute.
  1. [MessageContract]    
  2. public class Person    
  3. {    
  4.    [MessageHeader] public Operation Name;    
  5.    [MessageHeader] public string city;    
  6.    [MessageBodyMember] private Home Address;    
  7.    [MessageBodyMember] private Home Streat;    
  8.    [MessageBodyMember] public int age;    
  9. }  
The differences between DataContract and MessageContract:
  • Using DataContract, the service can expose the types that it interchanges. But the XML (SOAP) that is interchanged is not controlled (though impacted) by it.
  • While MessageContract can be used to explicitly define how the XML (SOAP message) will be structured.


This article gave a brief discussion of WCF Contracts.