Wednesday, 26 December 2012

A Beginner's Tutorial for Understanding Windows Communication Foundation (WCF) Part-1



Introduction
This article is an introduction to the Windows Communication Foundation (WCF). We will try to see the basic concepts behind WCF and will try to implement a small WCF service. We will also work out some small examples of how a WCF service can be consumed.
Background
Like I said in my earlier article on Web Services, communication between applications is very important. Web services provide an efficient way of facilitating communication between applications. But there are limitations with web services too. The major limitation with web services is that the communication can happen over HTTP only. A second limitation with web services is that it provides simplex communication and there is no way to have half duplex or full duplex communication using web services. 
Windows Communication Foundation (WCF) comes to the rescue when we find ourselves not able to achieve what we want to achieve using web services, i.e., other protocols support and even duplex communication. With WCF, we can define our service once and then configure it in such a way that it can be used via HTTP, TCP, IPC, and even Message Queues. We can consume Web Services using server side scripts (ASP.NET), JavaScript Object Notations (JSON), and even REST (Representational State Transfer).
The following table illustrates the differences between a web service and a WCF service:
Web Service
WCF Service
Communication can happen over HTTP only
Communication can happen over HTTP, TCP, IPC, or even MSMQ.
Only simplex and request-response communication is possible
It can be configured to have simplex, request-response, or even full duplex communication.
They work in an stateless fashion over HTTP and are hosted inside a web server like IIS
These can be hosted in many ways inside IIS, inside a Windows service, or even self hosted.
Note: Apart from these differences, there are other differences related to instance management, sessions, and data representation and serialization. We will not discuss these here as they tend to be digressing for beginners.


A WCF service can be visualized as:
   

Understanding the basics
When we say that a WCF service can be used to communicate using different protocols and from different kinds of applications, we will need to understand how we can achieve this. If we want to use a WCF service from an application, then we have three major questions:
  1. Where is the WCF service located from a client's perspective?
  2. How can a client access the service, i.e., protocols and message formats?
  3. What is the functionality that a service is providing to the clients?
Once we have the answer to these three questions, then creating and consuming the WCF service will be a lot easier for us. The WCF service has the concept of endpoints. A WCF service provides endpoints which client applications can use to communicate with the WCF service. The answer to these above questions is what is known as the ABC of WCF services and in fact are the main components of a WCF service. So let's tackle each question one by one.
Address: Like a webservice, a WCF service also provides a URI which can be used by clients to get to the WCF service. This URI is called as the Address of the WCF service. This will solve the first problem of "where to locate the WCF service?" for us.
Binding: Once we are able to locate the WCF service, we should think about how to communicate with the service (protocol wise). The binding is what defines how the WCF service handles the communication. It could also define other communication parameters like message encoding, etc. This will solve the second problem of "how to communicate with the WCF service?" for us.
Contract: Now the only question we are left up with is about the functionalities that a WCF service provides. Contract is what defines the public data and interfaces that WCF service provides to the clients.


Using the code
As an ASP.NET developer, we will find ourselves in the need of using a WCF service or perhaps write a WCF service. What we will do next is to implement a small WCF service and see how the ABC of WCF is to be implemented. We will then write a small application to consume this WCF service.
The service that we will create will provide arithmetic operations on a Pair data type. This Pair type will also be exposed by our service to the applications. We will simply provide addition and subtraction functionality for the Pair data type.
Note: We will host this WCF service inside an ASP.NET website, i.e., IIS hosting, and will be consuming it from an ASP.NET website. So let us create an empty ASP.NET website and then add a WCF service to it.
 

 
Creating a WCF Service
Let us start by looking at the Contract part of the WCF service. We need to expose a Pair data type from our service so this class will have to be decorated with the DataContract attribute. This attribute specifies that this data type can be used by consumers of this WCF service. Also, the public properties of this class will have to be decorated with the DataMember attribute to specify that clients can use these properties and to indicate that they will be needing serialization and deserialization.
[DataContract]
public class Pair
{
    int m_first;
    int m_second;

    public Pair()
    {
        m_first = 0;
        m_second = 0;
    }

    public Pair(int first, int second)
    {
        m_first = first;
        m_second = second;
    }

    [DataMember]
    public int First
    {
        get { return m_first; }
        set { m_first = value; }
    }

    [DataMember]
    public int Second
    {
        get { return m_second; }
        set { m_second = value; }
    }
}
Now we have specified the Data contract that our service is exposing. The next thing is to specify the service contracts and major operations of this service. Let us create an interface that will list the major functionalities provided by this service. We will then have to decorate this interface with the ServiceContract attribute to specify that this interface is being exposed by this service and can be used by the clients.
We will then have to write methods for all major operations provided by this service and decorate them with the OperationContract attribute to specify that these operations of this interface can be used by the clients.
public interface IPairArihmeticService
{
    [OperationContract]
    Pair Add(Pair p1, Pair p2);

    [OperationContract]
    Pair Subtract(Pair p1, Pair p2);
}
Now we have specified all the DataContract, ServiceContract, and the OperationContract of our small service. Now the major thing left in this service is to implement the functionalities. To do that, let's have a small class that will implement the interface we just created.
public class PairArihmeticService : IPairArihmeticService
{
    Pair IPairArihmeticService.Add(Pair p1, Pair p2)
    {
        Pair result = new Pair();

        result.First = p1.First + p2.First;
        result.Second = p1.Second + p2.Second;

        return result;
    }

    Pair IPairArihmeticService.Subtract(Pair p1, Pair p2)
    {
        Pair result = new Pair();

        result.First = p1.First - p2.First;
        result.Second = p1.Second - p2.Second;

        return result;
    }
}

Click here for next step.

No comments:

Post a Comment