Saturday, April 21, 2012

Introduction to Windows Communication Foundation

Today I am going to write about Windows Communication Foundation (WCF). I am pretty sure you all must have used WCF services already in your applications to build  the application communication. Since WCF is a topic of itself I will be only covering the basics here and it's up for you to discover the inner nice things which are in topics I am covering here.

Windows Communication Foundation (WCF) code named "Indigo" is an API which was introduced with .NET 3.0 and it is specifically designed for the process of building distributed systems.

A Distributed system can be two networked computers or even two executables in same computer that needs to exchange data. Let's call these two ends a client and a service computer. So these client and service computers sometimes can be running same operating system using the same programming language or may be different.

Prior to .NET 3.0 there were several distributed APIs used by windows software developers.
  • DCOM
  • COM+/Enterprise Services
  • MSMQ(Microsoft Message Queuing)
  • .NET Remoting
  • XML Web Services

XML Web Services

XML Web Services are what we used before WCF come to the picture. Web services are components on a Web server that a client application can call by making HTTP requests across the Web. Unlike traditional brower-based web applications, a web services provides a way to expose the functionality of remote components using standard web protocols.

The initial release of .NET has been released with the "System.Web.Services" namespaces with superior support for XML Web Services. In most cases the programmer just had to apply [WebMethod] attribute to each public method which he needs to expose to outside world.

These Web Services encodes it's data in simple XML and can be accessed using simple HTTP. Of course, XML Web Services is not a perfect distributed API for in-house applications where you could use a TCP based protocol and binary based formatting of data.

Windows Communication Foundation

WCF is a distributed API represented primarily by "System.ServiceModel" namespace. Using WCF, you can expose services to callers using wide variety of techniques. For example, if you build an in-house application where all connected machines are windows based, you can use various TCP protocols to ensure the fastest possible performance. You can also expose this same service with the XML Web Service based protocol to allow external callers to leverage its functionality, regardless of the programming language or the operating system.

The Basic Composition of a WCF Application

WCF applications mainly contains three main assemblies.
  1. WCF Service assembly - This dll contains the classes and interfaces that represent the overall functionality that we want to expose to external callers.
  2. WCF service host - This is the entity that hosts the service assembly.
  3. WCF client - This is the application that consumes the service through an intervening proxy.

The ABC's of WCF

The hosts and clients communicate with each other through these ABC's which I am going explain in a second.
  • Address - Describes the location of the service.
  • Binding - WCF ships with a many different bindings that specify network protocols, encoding mechanisms, and transport layer.
  • Contract - Provides a description of each method exposed from the WCF service.

     WCF Contracts

Understanding the role of a contract is the key to building a WCF service. A contract is a standard way of describing what a service can do. Meanwhile, it is platform independent. In WCF there are 4 types of contracts,
  1. Service Contract
  2. Data Contract
  3. Fault Contract
  4. Message Contract

     WCF Bindings

Bindings specify how a Windows Communication Foundation (WCF) service endpoint communicates with other endpoints.

     WCF Address

Once we have establish the contracts and bindings, final stage is to specify the address for the WCF service. From a high level, WCF addresses can specify the following bits of information.
  • Scheme - Transport protocol.
  • MachineName - The full qualified domain of the machine.
  • Port - This is optional in many cases, for example the default for HTTP binding is port 80.
  • Path - The path to the WCF service.
So the address would be the in the format of,

     WCF Endpoints

All communication with a Windows Communication Foundation (WCF) service occurs through the endpoints of the service.

So that's the basics of some theoretical things in WCF. If you got all these things hard to understand, try apply these with some practical programming of WCF. I am pretty sure you will be fine then.

Appreciate your feedback.

Happy Coding.