Introduction
The contract is a fundamental component of a WCF endpoint that encompasses essential details about the WCF service. It includes crucial information that outlines the capabilities and behavior of the Service, enabling clients to interact with it effectively. The contract acts as an agreement or protocol between the Service and its clients, defining the operations that can be invoked, data structures that can be exchanged, and exceptions that may occur during communication.
There are 4 types of Contracts :
- Service Contract
- Data Contract
- Fault Contract (Exception Handling)
- Message Contract
Service Contract
It includes the operations that clients can invoke on the services.
The Service contract includes two types of contracts.
- Service Contract: The Service is used to declare an interface in which the function or services provided are initiated.
- Operational Contract: The Operation contract is used to define a function or method inside a ServiceContract which can be accessed by the client
The below image shows a Service Contract IService1 in which a function GetData is initialized in the OperationContract.
Data Contract
The data contract defines the data types or structures which are used or exchanged between the client and the services provided. Data Contract plays a major role in WCF as it specifies the properties or fields of the data structures that will be serialized and deserialized during communication.
The Data Contract includes Data Member. The Data Member defines the individual pieces of data that make up a data contract.
For Example
This data contract defines a customer object with two properties: CustomerID and Name. The CustomerID and Name properties are public.
The Name property is a string that can be serialized and deserialized.
Also, they can be defined in the below way.
Fault Contract
The fault Contract defines the types of exceptions that can be thrown by a service operation and communicated to clients. It includes the exception that clients need to be aware of. Also, the errors or exceptions are passed to the client using it.
Here The Fault Contract of ExceptionMessage is declared with the OperationContract in a below way.
Defining Fault Contract of ExceptionMessage.
Here the exception is defined for any exception captured in Service using try and catch
Below, the exception of the Fault contract is implemented for throwing or capturing exceptions in Service.cs or on the client side.
Message Contract
A message contract is a WCF feature that allows you to define the structure of messages exchanged between your Service and its clients. Message contracts are used to define the data that is transferred and the format in which it is transferred.
They are used to control how the message body is structured and serialized. It helps in sending and accessing information in SOAP headers. By default, WCF automatically handles the creation of SOAP messages based on the defined DataContracts and OperationContracts in the Service.
\
Here [MessageContract] indicates that it will control the structure of the Message.
It includes two properties MessageHeader, which indicates that it should be included in the SOAP header of the Message, and MessageBodyMembare indicates that it should be included in the message body.
Summary
I hope this article helped you understand the basic concepts and types of Contracts in WCF.