My two cents on contracts in WCF

Take advantage of contracts, a standard followed in WCF to specify what your WCF service is supposed to do

Contracts in WCF

Contracts in WCF

WCF (Windows Communication Foundation) is a secure, reliable, and scalable messaging platform for developing services in .Net. When working with WCF, you first need to create a service contract and then define the service operations or operation contracts in it. You have many different types of contracts in WCF -- service contracts, data contracts, fault contracts, message contracts and operation contracts.

WCF services expose contracts to facilitate communication with the service consumers. A contract is a standard that is used in WCF to specify what the service is supposed to do. Contracts in WCF can be categorized into two distinct categories:

  •  Behavioral Contracts: In WCF we can have three behavioral contracts, namely ServiceContract, OperationContract and FaultContract.
  • Structural Contracts: These include DataContract and MessageContract.

ServiceContracts and OperationContracts

A ServiceContract is used to denote the service operations that are available for the service consumer at a particular service endpoint. In essence, the ServiceContract is used to specify the operations that are available for the service client to consume. A ServiceContract is defined using the ServiceContract attribute -- usually applied to an interface.

A ServiceContract can define the Message Exchange Pattern amongst the service provider and the service consumer and can have one or more service operations; these are known as operation contracts. An operation contract is used to define the signature of the service method and also transaction flow, direction of the service operation and also the fault contract(s) that may be associated.

The code listing given next illustrates how a typical service contract is defined.

[ServiceContract]

interface ITestService

{

     [OperationContract]

     string GetMessage();

}

public class TestService  :  ITestService

{

     public string GetMessage()

      {

           return "Hello World!";

      }

}

In the code listing shown above, the only operation contract in the service contract is GetMessage. Note how the attributes have been specified. Also, if you have a method in your service contract that doesn't have the operation contract attribute set, the method cannot be exposed by the service, i.e., the method cannot be consumed by the service consumer.

DataContracts, MessageContracts, and FaultContracts

A DataContract is used to describe the data that needs to be exchanged over the wire. It is used to specify how the data can be exchanged between the service provider and the service consumer. You can use the [DataContract] attribute to decorate your type so that the data can be serialized before it is passed over the wire. When defining data contracts, you would typically need to use data members to define the properties of the data contract.

The following code snippet shows how you can decorate a class with the [DataContract] attribute.

[DataContract]

public class Employee

{

 [DataMember]

 public string ID;

 [DataMember]

 public string FirstName;

 [DataMember]

 public string LastName;

}

A Message Contract is one that can be used to decorate the body of a message in WCF. In most cases you wouldn't need to use message contracts -- usage of data contracts would suffice. If you need a fine grained control on your SOAP messages you can take advantage of message contracts. You can use message contracts to access the SOAP headers.

You can use message contracts to specify the format of the SOAP message that needs to be required. While the MessageHeaderAttribute can be applied to the members that you would like to include in the SOAP headers, the MessageBodyMemberAttribute can be used to define the members that should be a part of the body of the SOAP message.

You can define a message contract by applying the MessageContractAttribute as shown below.

[MessageContract]

public class Transaction

{

  [MessageHeader] public DateTime date;

  [MessageBodyMember] public int amount;

}

A fault contract in WCF is used to define and propagate the errors that can occur when a service operation is executed. In essence, you can leverage fault contracts to pass the error messages to the service consumer when an error occurs in your service. Note that you can decorate your operation contracts using a fault contract -- a service operation contract can have one or more fault contracts associated. Here's an example that shows how fault contracts can be used. Note that two fault contracts named IDGFaultContractOne and IDGFaultContractTwo have been applied to the operation contract IDGContract in the code example given below.

[ServiceContract]

interface IDGContract

{

 [FaultContract(typeof(IDGFaultContractOne))]

 [FaultContract(typeof(IDGFaultContractTwo))]

 [OperationContract]

 string GetMessage();

 }

This article is published as part of the IDG Contributor Network. Want to Join?

From CIO: 8 Free Online Courses to Grow Your Tech Skills
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.