1.

What are the types of contracts in WCF?

Answer»

The WCF includes five types of contracts as follows:  

  • Service contract: It describes what operations the service exposes to the OUTSIDE world. This is the interface to the WCF service and it lets the outside world KNOW what the service can do.

Example: 

[ServiceContract]interface IMyContract{[OperationContract]string MyMethod();}class MyService : IMyContract{public string MyMethod(){return "Namaste";}
  • Operation contract: It specifies the method by which client and server exchange information. It ALSO states what functionality the client will receive, such as adding, subtracting, etc.

Example: 

[ServiceContract] interface ICustomer { [OperationContract] Response AddNew(string CUSTOMERNAME); Response Delete(int customerID); }
  • Data contract: It is a formal agreement between the server and the client that involves high-level security for the exchange of data between the two. Format, STRUCTURE, and type of data exchanged between parties are defined by it. Serialization and deserialization of data are also described.

Example: 

[DataContract]class Person{ [DataMember] public string ID; [DataMember] public string Name;} [ServiceContract] interface IMyContract{ [OperationContract] Person GetPerson(int ID);}
  • Message contract: A message contract specifies the structure of the message and how the serialization should be performed. With it, you control everything from the content of the SOAP header to the structure of the SOAP body.

Example: 

[ServiceContract] interface ICuboidService { [OperationContract] [FaultContract(typeof(CuboidFaultException))] CuboidDetailResponse CalculateDetails1(CuboidInfoRequest cInfo); [OperationContract] [FaultContract(typeof(CuboidFaultException))] CuboidDetail CalculateDetails2(CuboidInfo cInfo); [OperationContract] [FaultContract(typeof(CuboidFaultException))] CuboidDetail CalculateDetails3(int nID, CuboidDimension cInfo); } 
  • Fault contract: It generally specifies how errors or defaults that are raised by the service are handled and propagated to clients. There can be zero or more fault contracts associated with an operation contract.

Example:  

[ServiceContract]interface IMyContract{ [FaultContract(typeof(MyFaultContract1))] [FaultContract(typeof(MyFaultContract2))] [OperationContract] string MyMethod(); [OperationContract] string MyShow();}


Discussion

No Comment Found