Search Chirayu Software Solutions Blog

Thursday, January 17, 2013

wcf basics


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 WCFservice can be consumed.


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 ServiceWCF Service
Communication can happen over HTTP onlyCommunication can happen over HTTP, TCP, IPC, or even MSMQ.
Only simplex and request-response communication is possibleIt 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 IISThese 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.
WCF service can be visualized as:
wcf artcile image

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 WCFservices 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 WCFservice?" 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.

Post a Comment