Loading ...

Contract in WCF ? (Part # 3) | CodeAsp.Net

Contract in WCF ? (Part # 3)

0 (0votes)

General meaning of contract means mutual understanding between each other. Contracts ensure that each party is aware of what his or her efforts will return.

WCF uses the concept of contracts to define the service and its operation as a whole, to explain the data which are transferred to other application.

WCF contains 4 type of contract

1. Service contracts

2. Data Contracts

3. Message Contracts

4. Fault Contracts

Service Contracts:

  • The service contract defines the functionality which is exposed by your service to the outside World. The functionality is expressed in the form of service operations.
  • The service contract further defines the message exchange pattern (Request/

Reply, One – Way, Duplex) for each service operation.

Service contracts is the combination of

  1. Service Contract
  2. Operation Contract
  1. Service Contract: It is used to define the interface
  2. Operation Contract: It is used to define method inside the interface
        public interface IService
        int add(int a, int b);


Data Contracts
The Data Contract defines the structure and content of the information that is exchanged between the client and the service.
It is the formal agreement between client and service which data is going to be exchanged.
Data contract can be explicit or implicit. For example, int, string etc has an implicit data contract.
Data Contracts also contains two type of contract i.e
DataContract: It is used to define the Class
DataMember: It is used to define the Properties.


public class Emp
public int Id { get; set; }
public string EmpName { get; set; }
public string EmpSal { get; set; }

Message Contracts
• WCF uses SOAP message for communication.
• A message contract is used to gain greater control of the SOAP header and/or the SOAP body.

• The message contract is used to directly interact with the SOAP message
(header and body), not just the method body.
Syntax for message contract is like this


public class EmpInfo
public int Id { get; set; }
public string EmpName { get; set; }


When to Use MessageContract  instead of DataContract ?

In the following situation we can use message contract

  •  When we need a higher level of control over the message, such as sending custom SOAP header,then use    MessageContract instead of DataContract
  •  If our communication partner which requires a very specific format then we have to tweak our SOAP messages to match that given layout exactly.

  •  If our communication partner would like to have a custom security header with username and hashed password. Then we have to use message contract.


Fault contracts 

1.It defines which errors are raised by the service, and how the service handles and propagates errors to its clients.
2. Fault contract is the contact between service and client about what exception can be thrown from service to client.
3. It is used to handle the error which is raised by the service.
Syntax for fault Contract is like this


public interface IService
double divide(double a, double b);


Comments (2)

good post.
Thank you.

Top Posts