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
- Service Contract
- Operation Contract
- Service Contract: It is used to define the interface
- Operation Contract: It is used to define method inside the interface
[ServiceContract]
public interface IService
{
[OperationContract]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.
OR
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.
[DataContract]
public class Emp
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string EmpName { get; set; }
[DataMember]
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
[MessageContract]
public class EmpInfo
{
[MessageHeader]
public int Id { get; set; }
[MessageBodyMember]
public string EmpName { get; set; }
}
When to Use MessageContract instead of DataContract ?
In the following situation we can use message contract
1. when we need a higher level of control over the message, such as sending custom SOAP header, then use MessageContract instead of DataContract
2. If our communication partner which requires a very specific format then we have to tweak our SOAP messages to match that given layout exactly.
3. 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
[ServiceContract]
public interface IService
{
[OperationContract]
[FaultContract(typeof(DivideByZeroException))]
double divide(double a, double b);
}
Hi Chandra,
About using MessageContract; have you ever used it in any of your projects in place of a DataContract? If so, would you please let me know at a high level what the requirements was?
I think MessageContract requires more work versus a DataContract. So I am trying to find a valid example
Thanks !!!
Hi
I don’t have use MessageContract in my project still now. Generally i used to use to data contract. I donot have get situation to use message contract. But with in few days i will write sample code using “MessageContract”.