Working with Contracts

Working with Contracts

DOI: 10.4018/978-1-5225-1997-3.ch006
OnDemand PDF Download:
$30.00
List Price: $37.50

Chapter Preview

Top

Introduction To Contracts

A contract is similar to the real life contract we sign. In WCF it is an agreement between client and service to restrict the boundary of the service operations. It is also important to know about contract as it is related serialization too. A service and client must agree on contract for communication to happen. There are mainly three types of contracts available in WCF: Service Contract, Data Contract and Message Contract. Each of these contracts is explained in details in the following sections.

Service Contract

A service contract contains a list of operations made available to the client. It also defines:

  • Message pattern (Oneway, request-reply, duplex)

  • Protocol and serialization format

  • Datatype of the operations

  • Grouping of the operations

A service contract is represented by [ServiceContract] annotation. The operations are labelled as [OperationContract] annotation. The example of service contract is shown in Example 1.

Example 1. Example of Service Contract

   [ServiceContract]
public interface ICalculator
{
[OperationContract]
int add(int no1, int no2);
[OperationContract]
int sub(int no1, int no2);
[OperationContract]
int mul(int no1, int no2);
[OperationContract]
int div(int no1, int no2);
}

It is required to have at least [OperationContract]. The operations having a label [OperationContract] are available to the client. The code in Example 1 is re-written again after removing [OperationContract] from several operations as shown in Example 2.

Example 2. Service Contract After Removing [OperationContract] From Some

                       Operations:
[ServiceContract]
public interface ICalculator
{
int add(int no1, int no2);
[OperationContract]
int sub(int no1, int no2);
int mul(int no1, int no2);
[OperationContract]
int div(int no1, int no2);
}

As shown in Example 2, the operations add and mul are not having a label [OperationContract] so a client program cannot consume these methods The [ServiceContract] is generally an interface and it must be implemented by a service class as it is interface. The implementation of service contract of Example 1 is shown in Example 3.

Example 3. Implementation of Service Contract using Service Class

public class Calculator: ICalculator
{
public int add(int no1, int no2)
{
return no1 + no2;
}
public int sub(int no1, int no2)
{
return no1 - no2;
}
public int mul(int no1, int no2)
{
return no1*no2;
}
public int div(int no1, int no2)
{
return no1 / no2;
}
}

A class can work as [ServiceContract] too. In this situation interface is not required and a class must be labelled [ServiceContract]. The example of a class being a [ServiceContract] is depicted in Example 4.

Example 4. Class as Service Contract

[ServiceContract]
public class Calculator
{
[OperationContract]
public int add(int no1, int no2)
{
return no1 + no2;
}
[OperationContract]
public int sub(int no1, int no2)
{
return no1 - no2;
}
[OperationContract]
public int mul(int no1, int no2)
{
return no1*no2;
}
[OperationContract]
public int div(int no1, int no2)
{
return no1 / no2;
}
}

In this example no interface is required and class behaves like a service contract and service class both. Therefore while creating a configuration file you need to provide contract as class name and service name as class name too. The way provided in Example 4 leads to ambiguity and it is not a better programming practice to directly write the code without interface, this method of programming should be avoided. So we will not use the method presented in Example 4. A service contract has several parameters which is discussed in section below.

Complete Chapter List

Search this Book:
Reset