browse by category or date

Windows Communication Foundation (WCF) service exposes contracts to its clients. Contract here refers to the platform-neutral and standard way of describing what is available/can be done in the service. Below are the types of contracts:

1. Service contracts

It describes what operations the client can call/perform on the service.

[ServiceContract]
interface ILibrary
{
   [OperationContract]
   bool Borrow(Book arg1, User arg2);

   [OperationContract]
   bool Return(Book arg1, User arg2);
}

2. Data contracts

It defines the data-type which will be passed, to and from the service. Built in types (int, string) will be implicitly defined. But complex/custom data type must be explicitly defined. The custom/complex data type must be serializable.

[DataContract]
struct Book
{
   [DataMember]
   public int Id;

   [DataMember]
   public string Title;

   [DataMember]
   public string ISBN;
}

3. Fault contracts

It defines what errors will be raised by the service, how it handles and propagates the error to its clients. Fault contract can only be declared on operation contract which returns value.

[ServiceContract]
interface ILibrary
{
   [OperationContract]
   [FaultContract(typeof(InvalidArgumentException))]
   bool Borrow(Book arg1, User arg2);

   [OperationContract]
   [FaultContract(typeof(InvalidArgumentException))]
   bool Return(Book arg1, User arg2);
}

4. Message contracts

A message is the packaged information which is sent to/from service. The package’s outer layer is the envelope. Inside the envelope, there is a header, and a body (similar to data defined by data contracts). A message contract allows the service to interact directly with messages (e.g. accessing information defined in the header)

[MessageContract]  
public class BankingTransaction  
{  
  [MessageHeader] public Operation operation;  
  [MessageHeader] public DateTime transactionDate;  
  [MessageBodyMember] private Account sourceAccount;  
  [MessageBodyMember] private Account targetAccount;  
  [MessageBodyMember] public int amount;  
}

Which will generate the following SOAP envelope:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  
  <s:Header>  
    <h:operation xmlns:h="http://tempuri.org/" xmlns="http://tempuri.org/">Deposit</h:operation>  
    <h:transactionDate xmlns:h="http://tempuri.org/" xmlns="http://tempuri.org/">2012-02-16T16:10:00</h:transactionDate>  
  </s:Header>  
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
    <BankingTransaction xmlns="http://tempuri.org/">  
      <amount>0</amount>  
      <sourceAccount xsi:nil="true"/>  
      <targetAccount xsi:nil="true"/>  
    </BankingTransaction>  
  </s:Body>  
</s:Envelope> 

Message contracts sample is taken from HERE.

GD Star Rating
loading...
Types of Contracts in WCF, 3.0 out of 5 based on 1 rating

Possibly relevant:

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Incoming Search

.net, wcf, web service

No Comment

Add Your Comment