2nd
JUL
Windows Communication Foundation - An Overview
Posted by Sambeet Patra under .NET Software Development, Service Oriented Architecture (SOA), Software Architecture, Software Development Platforms, Web Services, Workflow Systems
Windows® Communication Foundation (WCF) is the unified communication programming model in the .Net framework for building service-oriented applications. In this article I will explain the facets of WCF. I will also build a simple WCF service to demonstrate the concepts.
Why WCF?
WCF is the part of the .NET Framework. It unifies the various communications programming models existing in the Microsoft platform into a single model.
For the past few years, Microsoft distributed solutions have mostly been composed of ASP.NET Web Services / Web Service Enhancements (WSE) extensions / Microsoft Message Queue (MSMQ) and .NET Remoting. Each of these technologies has its own benefits and drawbacks. WCF does not attempt to replace these, nor does it provide a solution that tries to compete with these technologies. WCF is merely a framework that
- provides a base to develop and expose functionalities through any of the above technologies
- empowers the developer to write code that deals with business logic only and “configure” [instead of write code any more] WCF to host the code using one of the one of these distributed frameworks.
Further, it is important to understand that there is a learning phase associated with WCF unlike some of the other .Net technologies [ASP.Net / Web Services etc]. But there is a good enough reason for this. Most of the distributed technologies discussed earlier provide inbuilt features that help developers write code to support specific scenarios. But WCF goes a step further; it mandates standards that need to be followed to write services. Also, since it provides support for most of the existing distributed technologies, the developer needs to understand how these technologies have been conceived to co-exist. Simply put, while other technologies provided “features” and “libraries” and let developers dictate the end result, WCF enforces some well thought out best practices, thereby reducing possible mistakes during conceptualization / development of distributed applications.
WCF Architecture
The following image shows the basic architectural layout for WCF.

- Service Contracts: We will explore this in detail later on. Contracts define the details of the functionalities that are exposed in a service.
- Application Implementation: This is the implementation code for the contracts. This is written by developers.
Note that once you have defined the contracts and written the implementation of the contracts, all other aspects of hosting the service can be configured using the WCF framework. For most of the standard requirements for hosting services, WCF can be configured to provide the right solution without a single line of coding. We will explore the components of the WCF framework in detail. Here is a very high level description of what WCF provides.
- Runtime: As the name suggests, WCF framework provides extensive runtime support for various hosting scenarios. These include requirements like Transaction / Remote Invokation / Life Cycle Management / Exception handling etc.
- Messaging: This layer consists of channels that process the incoming / outgoing messages one by one. This layer is responsible for implementing the WS standards / encoding / support for protocols like HTTP and TCP. Developers familiar with the WSE (Web Service Enhancements) tool will find that this is really the next improved version of the WSE functionalities.
- A service is a program; like other programs, it needs to be hosted either as an independent executable or using a hosting service. The activation features deal with this aspect. WCF Services can also be hosted, or run in an executable managed by an external agent, such as IIS or Windows Activation Service (WAS). A WCF service can also be run automatically as a Windows service.
Service Contracts
Services publish contracts that provide the constituents of the intended functionalities. A contract, in WCF is nothing but an improvisation of interfaces. In fact a contract is an interface with a specific attribute so that the WCF framework recognizes it. The following code snippet demonstrates how a contract is declared.


In the following sections, we will explore how the implementation can be exposed using WCF.
End Points
WCF exposes services to the outside world via an endpoint. The end points define the characteristics of the WCF hosted service.
The ABC of End Point:
- Has an Address. This is a uri that specifies the location of the endpoint.
- Has a Binding associated. A binding specifies what protocols are used to access the service. WCF includes predefined bindings for common communication standards such as SOAP over HTTP, SOAP over TCP etc.
- Is linked to a Contract. The end point makes this contract available to the service consumers.
When a client wants to access the service via an endpoint, it not only needs to know the Contract, but it also has to adhere to the binding specified by the endpoint. Thus, both client and server must have compatible endpoints. Keep in mind that it is possible to define end points that make the service compatible with consumers of the traditional ASP.Net web services.
Understanding Bindings
As I mentioned earlier, WCF provides a framework to write distributed services and it provides a platform for the developers to focus on business functionality without worrying about the underlying technology. So how does WCF provide the platform features? The key here is “Binding”. A binding consists of one or more binding elements. Each binding element handles a specific non-functional aspect of a service, such as whether it supports transactions, the kind of security the service incorporates, the standards for data transport etc. Binding elements are grouped together to create a binding that is required for the service we are developing.
So how does binding add value?
- WCF provides several out of the box implementations of bindings which represent some of the commonly used techniques. Think of all the previous distributed technologies in the Microsoft world. WCF will have one or more binding implementations representing the strength of the each of these technologies.
- Use of bindings does not require any coding unless you want to write your own specific binding implementation.
- Interoperability is one of the key requirements for services. Chosing the right binding will make the service interoperable without writing any code.
Now, let us look at some of the standard bindings provided by WCF:
- BasicHttpBinding: This binding is based on the WS-I Basic Profile 1.1. It uses the HTTP / HTTPS transport protocols. Use this binding to maintain compatibility with client applications previously developed to access ASMX-based Web services.
- WSHttpBinding: This binding implements the WS-* specifications to implement transactions and secure, reliable sessions. It supports HTTP / HTTPS protocols. Messages can be encoded as XML or by using the Message Transmission Optimization Mechanism (MTOM). MTOM is a preferrable mechanism for messages that contain binary data.
- WSDualHttpBinding: This is similar to WSHttpBinding, however it is suitable for bi-directional communication between the client and server without synchronizing the individual messages [as opposed to a typical request-response model].
- NetTcpBinding: This TCP transport protocol and messages are encoded using binary encoding. While is has better performance compared to HTTP protocol, it is not as interoperable as HTTP based protocols. It supports transactions and secure communications.
- NetMsmqBinding: This binding uses Microsoft Message Queue (MSMQ) as the underlying messaging platform. This handles scenarios where messages are delivered asynchronously [in other words, messages are queued]. Hence, the server need not be running all the time for the protocol to function.
An Example
We still need to discuss a few other aspects of WCF framework. But let us build a sample application with whatever we have discussed so far. We will create a WCF service for the tax service contract we had created earlier.
I have presented the steps to create the service below. I assume that you are familiar with Visual Studio .Net and have Visual Studio 2005 with .Net 3.0 add on or Visual Studio .net 2008 installed in your computer.
- Create a new project of type “WCF Service Library”.

- Once the projected is created, add the following classes. This represents the sample contract we had created earlier:



Compile the project once and make sure that it compiles properly. Till now we have defined a contract and implemented it. Now, we will write the necessary configuration to implement the service. For our example, we will host the service in IIS and expose it as a basic HTTP web service. [I will explain hosting later on].
- Open IIS administration console and create a virtual mapped to the folder where you have the project. Add web.config file to the project. Note that this may sound a bit confusing and you may wonder why we did not create a web service project in the first place! I am just trying to make a simple point here. WCF DOES NOT HAVE to be hosted in IIS. It is just one of the options available.


- Now, add a file named TaxService.svc to the project. The content of the file should be as shown below.

-
The svc file stores the details of which assembly and class implement the contract. In this case I have mentioned the TaxServiceImpl class in the mytaxservice assembly [which is the output of my project]. This way, we completely detach the contracts from the implementation and we also abstract it from the outside world. If we ever come up with a different implementation for the same contract, we just need to update the svc file.
- Since the service is being hosted in IIS, make sure you reconfigure the project property to define the output under the bin\ folder as opposed to bin\Debug. Also make sure that you configure the virtual to use the right version of ASP.Net.

- Now it is time to define the end point for the service. We will just be adding a few items in the config file to define the end point and then expose the service to the consumers. Here is the content of the web.config file.

- The <serviceModel> represents the configuration details for WCF Web service.
- <services> includes the details of all the services offered by the deployment. The name attribute indicates the namespace and the class that implements the service.
- A service can be exposed in different manners. This is achieved by including a number of endpoints that expose the service with specific bindings.
- The address attribute in the <endpoint> tag is kept empty as we want to keep the address relative to the address of the virtual for now. The contract attribute is self explanatory as it mentions the contract that the endpoint exposes. The binding element indicates the binding of the contract. Here we are using basic HTTP, which is one of the many readily avaiable bindings in the WCF framework. In other words, merely choosing this binding sets up the service as a simple web service we used to develop using the ASP.Net asmx files. Once you get into the finer details of WCF, you will realize that changing the behavior of the service is all about coming up with the right configuration.
The Behavior Tag
Behaviors modify or extend the functionality of a service. In the example, the serviceMetadata tag indicated that the Service publishes metadata. Also, the security behavior controls impersonation and authorization, while the transactions behavior controls enlisting in and auto-completing transactions.
In the example we used the serviceMetadata tag to ensure that httpGetEnabled is set to true. This enables us to view the published metadata for the service. To understand what this means, browse to http://localhost/mytaxservice/TaxService.svc and view the metadata for the Tax Service. Now set the value of httpGetEnabled to false and revisit the above url. You will notice that a message comes up stating that the service metadata is not available.
The Client?
Note that we did not write a sample client while implementing the sample application. So, what is different for a WCF client? Does it need to be aware of the fact that the service is hosted in WCF? Well, in stead of answering this question with a Yes or No, let me reiterate two distinct goals of WCF:
- It provides a framework to write service oriented application. Per definition, services are supposed to be interoperable and hence the client does not need to know the implementation technology of the service. WCF has conformed to this behavior and writing a client for a service created by WCF is no different from writing the client for a service written by any other technology.
- WCF also brings in all the strengths of various Microsoft technologies. Certain technologies are not necessarily interoperable. In such cases the clients needs to know at least the endpoint details of the service. In other words, you will need to write WCF-aware clients that have end points defined to communicate with the service. Needless to say, the binding of the client endpoint has to match the service endpoint.
Hosting of WCF
A host application has to perform a number of tasks. Some common requirements include
- Starting and stopping the service
- Listening for requests from a client application and directing them to the service
- Sending any responses from the service back to the client applications
- Providing diagnostics features
You can use the following options to host WCF services.
- IIS: We already saw this in the sample application. The IIS provides a number of readymade features. It provides automatic activation, diagnostics, on demand compilation and the ability to change application while it is live.
- Create a custom .Net application that the user runs to start and stop the service. While this gives you ultimate control you have to write all the code to provide the functionalities for hosting.
- Host the WCF service in a Windows service so that the WCF service is available as long as Windows is running. Also this is a favorite for administrators as they are well versed with Windows services.
- Use Windows Activation Services, or WAS if you are using IIS 7.0
Conclusion
In this article I presented a high level overview of WCF. WCF offers many configurable and extensible features for developers and support personnel to customize every aspect of service deployment. Even though it takes a while to understand the various components of WCF, the knowledge pays rich dividends as it empowers you to host production ready applications with the least amount of effort.
Useful References
http://msdn.microsoft.com/en-us/netframework/aa663324.aspx
http://netfx3.com/content/WCFHome.aspx
About the Author
The author is a seasoned Electronic Business Solutions architect at Silicus and has led several large scale software development projects for e-Commerce, CRM and Business Process Management solutions across multiple industries.

Added. Nice work on this one. Btw, my blog is dofollow, stop by and grab a link. Bompa