A contract is a Standard way of describing way a service can do. In the real world contract is an agreement between two or more parties about doing service or some other things. Similarly in WCF it is the agreement between the Service and Clients, through which message can be passed to and from service endpoints.
The Contract consist of three major types
1) Service contract
2) Data contract
3) Message contract
4) Polices and binding
Service contract is a class or an interface that specifies the direction and type of message. This contract describes the operation, that can be avail by our world. Example: – To know the current market price based on the company shares. So Service contract is a gate way to the external application to avail the functionality and operations and there must be atleast one operation contract.
Data Contract describe data structures that are used by the service to communicate with clients. It also describes the custome data type which is exposed to the client. The system define datatype are automatically identify by the client, because it is already define in XML schema defination language doument(XSD), but custom created class or datatype cannot be identified by the client.
Example:- Product datatype, by using DataContract client can understand that Product is the custom datatype for a method that is returning or passing the parameter.
If ‘DataMember’ attributes are not specified for a properties in the class that property can’t be passed to or from web service.
Message contract allow the service to interact directly with messages. It can be typed or untyped and are useful in interoperability. Message contract provide control over the soap header and bodies. MessageContracts are used to explicitly describe the soap message format.
WCF runtime provide SOAP message formatting for comminication between client and service.If it is not meeting your requirements then we can create our own message format. This can be achieved by using Message Contract attribute.
Policy and Binding
Policies and bindings stipulate the conditions required to communicate with a service. For example, the binding must (at a minimum) specify the transport used (for example, HTTP or TCP), and an encoding. Policies include security requirements and other conditions that must be met to communicate with a service.