WCF - Data Contract


  WCF - Data Contract   

  • Data Contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. 
  • That is, to communicate, the client and the service do not have to share the same types, only the same data contracts

Interaction of Operation Contract with Data Contract

Data Contract :

  • Data contract defines the data type of variable exposed to the client. 
  • Message with primitive data types are automatically serialized for exchange.
  • User defined types need an explicit Data Contract for serialization process and hence users using [DataContract] and [DataMember] attributes.
  • So to conclude, the DataContract makes the client to be aware of variable’s data type that are coming from or passed to a Service Contract.

Service Contract :

  • A service contract defines the operations which are exposed by the service to the outside world.
  • It  is the interface of the WCF service and it tells the outside world what the service can do. 
  • It may have service-level settings such as the name of the service and namespace for the service.

Operation Contract :

  • An operation contract is defined within a service contract such that the parameters and return type of an operation has been declared.
  • It can also define operation-level settings like transaction flow of the operation, the directions of the operation (one-way, two-way, or both ways) and fault contract of the operation.

Fault Contract :

  • A fault contract defines errors raised by the service, and how the service handles and propagates errors to its clients. 
  • An operation contract can have zero or more fault contracts associated with it.

Serialization / Deserialization :

  • Serialization is a process used at sender’s place to transform an object into a sequence of bytes for transmission over the network. 
  • Deserialization again transforms the sequence of bytes received,back to the object form
  • A reference for System.Runtime.Serialization is required in the project code to use DataContract and DataMember attributes.

Serialization / Deserialization

Sample Code :

[DataContract]
    public class CompositeType
    {  
        bool boolValue = true;
        string stringValue = "Hello ";
        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }
        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value;
        }
    }

Code Explanation :


    The WCF application automatically creates a Service Interface IService1.cs file on opting a new project.

  The file IService1.cs defines a DataContract and the class name as CompositeType.

   The CompositeType data contract has a [DataMember] named BoolValue, which is a property used to get set values.

  The CompositeType data contract has another [DataMember] named   StringValue, which is a property with get set values.


Operation-Service-Data Contracts

  • As per the above diagram, the operation/service contract, interacts with the service class to access data from data contract.