Contract provides Interoperability to communicate with the
client. Contract basically is a type of contract between client and service.
In WCF we have below
types of contract.
Service Contract
Operation Contract
Data Contract
Message Contract
Fault Contract
Service
Contract: A service contract defines
the operations which are exposed by the service to the outside world. For Exp:
[ServiceContract]
interface IEmployee
{
[OperationContract]
EmployeeDetail GetEmployee(EmployeeInfo
emp);
}
Operation
Contract: We define Operation Contract inside Service Contract.
[ServiceContract]
interface IEmployee
{
[OperationContract]
EmployeeDetail GetEmployee(EmployeeInfo
emp);
}
Data
Contract: Data Contract Describe the Data to be exchanged.
[DataContract]
public class EmployeeInfo
{
}
Message
Contract: Message contracts give more control over the actual SOAP message
for a service operation request or reply. A message contract defines the
elements of the message (like as Message Header, Message Body), as well as the
message-related settings, such as the level of message security.
[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
EmployeeDetail CalPrice(Employee request);
}
[MessageContract]
public class Employee
{
[MessageHeader]
public MyHeader SoapHeader { get; set; }
[MessageBodyMember]
public EmployeeInfo Employee { get; set; }
}
[DataContract]
public class MyHeader
{
[DataMember]
public string EmpID { get; set; }
}
[DataContract]
public class EmployeeInfo
{
[DataMember]
public string EMP_Name { get; set; }
[DataMember]
public sring Country { get; set; }
[DataMember]
public string City { get; set; }
[DataMember]
public string Mobile{ get; set; }
}
Fault
Contract: Fault Contract defines the error raised by the service.
[ServiceContract]
interface IEmpContract
{
[FaultContract(typeof(MyFaultContract1))]
[FaultContract(typeof(MyFaultContract2))]
[OperationContract]
string GetEmployeeAddress();
[OperationContract]
string ShowEmployee();
}