WCF Programming Methodology

WCF Programming Methodology

DOI: 10.4018/978-1-5225-1997-3.ch003
OnDemand PDF Download:
$37.50

Chapter Preview

Top

Wcf And Object Oriented Programming (Oop)

In the previous chapter, you learned about how to create first basic WCF service. In this entire book we will use C# language to develop the WCF service. The C# is object oriented programming language; therefore we can say that in WCF object oriented programming and Service Oriented Architecture (SOA) concepts are blended. To develop WCF service it is required to have strong fundamental knowledge of OOP concepts including interface and class. The comparison of a service contract and an OOP interface is shown in Table 1.

Table 1.
Comparison between WCF service contract and normal OOP interface
A WCF Service ContractNormal OOP Interface
[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);
}
public interface ICalculator
{

int add(int no1, int no2);

int sub(int no1, int no2);

int mul(int no1, int no2);
int div(int no1, int no2);
}

As shown in Table 1, the only difference between both methods that the attributes are applied in WCF program which are absent in the interface. Therefore, if the attributes are removed from WCF program, it is exactly same as the interface mentioned in the right side. Moreover, we can say that WCF is using OOP concepts to build Service Oriented Applications (SOA).

Top

Programming Methods In Wcf

There are different programming methods available in WCF. Each method can have its own advantages and disadvantages. There are three programming methods in WCF: (A) Declarative (B) Explicit (B) Configuration. These methods are explained below:

(A) Declarative

In this method the annotations/ attributes are applied as label on the top of the particular part of the code. It is also useful to specify parameters to enable and customize certain features in the program as per the developer’s choice. The example of the declarative method is shown in Example 1.

Example 1. Service contract with declarative methods:

[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);
[OperationContract (IsOneWay=true)]
void display(int no1, int no2);
}

Here in above example, [ServiceContract] and [OperationContract] are considered as attributes or labels. There no explicit coding is required to declare them. These labels are sufficient and the program will consider them as the label mentioned at the top of them. It is also possible to specify parameters to this attribute as it shown in fifth operation in Example 1. The parameter is “IsOneWay” and its value is given as true to indicate that the operation below this label is one way operation. Another example of declarative method in which attribute is mentioned over the service is shown in Example 2. As we can see in Example 2, an attribute is mentioned over the service class which is service behaviour and it helps to control the service runtime. In this example the parameter is not mentioned, but it is possible to provide the parameter using parenthesis just as the previous example.

Example 2. Service class with declarative method:

[ServiceBehavior]
public class Calculator: ICalculator
{
……
……
}

Complete Chapter List

Search this Book:
Reset