Revisiting WCF Contracts
The concept of a contract is a core foundation to WCF. Address and binding are also important
pieces, but they track closer to an administrator’s set of responsibilities. Contracts, on the
other hand, are firmly rooted in the developer’s space.
WCF defines three different types of contracts:
• Service contract : As we mentioned earlier, this defines the service operations and the
input and output parameters of each operation. Every service must have one associated
service contract and may have more.
• Data contract: This defines the data structure WCF uses to serialize and deserialize an
instance of a complex type. Data contracts must be associated with every complex type
exposed by a service operation as parameters or return values.
• Message contract: This enables you to explicitly define the layout of a message; for
example, what goes in the header versus what goes in the body of the message.
The following sections provide some more details about each of the contract types.
Using Service Contracts
WCF provides three attribute types that together allow you to define a service contract:
ServiceContract, OperationContract, and BindingRequirements.
You’ve already seen simple examples of ServiceContract and OperationContract. Under-
stand that each of these provides additional parameters that allow you to further refine the
contract. For example, consider this service contract, which shows some ServiceContract and
[ServiceContract(Namespace="http://indigorocks/", Name="CustomerService")]
interface ICustomerService
{
[OperationContract()]
CustomerData GetCustomerByEmail(string email);
[OperationContract(IsOneWay=true)]
void SaveCustomer(CustomerData cust);
}
Namespace and Nametogether enable you to explicitly specify the namespace and name of
the contract. By default, WCF uses the full interface or class name. Also notice the IsOneWay
parameter is set in the OperationContract to indicate that the SaveCustomermethod is a one-
way operation with no return values.











0 comments:
Post a Comment