LogoASPAlliance: Articles, reviews, and samples for .NET Developers
Fundamentals of Windows Communication Foundation
by Keyvan Nayyeri
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 42462/ 89


In first part of my tutorials about Windows Communication Foundation I gave an introduction.  WCF is an infrastructure for distributed systems that is produced by Microsoft and most of the differences that you will see between Object Oriented Architecture and Service Oriented Architecture come from this "Distributed Systems."

Service Oriented applications have some different fundamentals in comparison with Object Oriented applications.  Therefore if you are not aware of these fundamentals then you need to learn them in order to be able to work with Service Oriented technologies.  So in WCF we frequently deal with some simple but important fundamentals that make the basics of this technology.

In this article I want to talk about all the fundamentals that you need to know to start working with Windows Communication Foundation.  After reading this article, you will know the meaning of many common words in WCF articles, books and documentation.

Before stepping into the main body of this article I recommend you read the first part of these tutorials.  In the first part we learned that three things make a WCF application: Services, Clients and Intermediaries.  In this part we will follow previous part by introducing more concepts and fundamentals.


Message is the key in WCF.  Without messages we do not have anything!  As I stated in the first part, services, clients and intermediaries communicate with each other via messages.  They send and receive messages in order to do things.


First, it is better to give a background about message structure in WCF.  Each message in WCF is an XML document (especially SOAP Envelope) that consists of two main parts: headers and body.

Each message may contain one or more headers, but only has one body.  Headers are some means that infrastructures or developers consume and they are mainly used to represent some information about the message itself.  The body is what we (developers) should care about because most of our codes will be applied to the body.  The body is where our data is located.


Based on our needs in WCF we deal with messages in one of following three patterns:

·         Simplex: This is a one way messaging pattern.

·         Request-Reply: This is a two way asynchronous messaging pattern.

·         Duplex: This is a two way synchronous pattern.

Let us take a look at these three patterns in more detail.

In simplex pattern, which is a one way asynchronous pattern, you send a message from client to service and nothing more (Figure 1).

Figure 1:

In the request-reply pattern the client sends a request to the service and waits until it returns a reply back.  Nothing can happen in this between service and client.  Request-reply is a common pattern for internet users and everyday you deal with this pattern on internet.

Figure 2:

In the duplex pattern the client and service can communicate with each other regardless of anything that is going on between them so a client can send a message to the service and the service can send a message to the client.  Here, neither the client nor service needs to wait.

Figure 3:


Channel is actually a bridge between a client and a service.  Messages can travel between clients and services through channels.  There are four main types of channels:

·         Simplex Input

·         Simplex Output

·         Request-Reply

·         Duplex

But how a channel can be created?  The first time when a message is going to be sent (only at this time) the client tries to create a channel on a service endpoint (you will read about it later in the next section) based on the type of channel that is required and an address.  If the service is listening on that address then a channel will be created.  Here there is a point: you do not have any channel until the first message is sent.  When communication between client and service ends, the channel will be destroyed.

A channel is a queue of some features that make up its properties, such as security, session or messaging pattern. There are different types of each feature available, but you must choose a compatible combination to be able to create a channel.  Here I describe these features.

Each channel type supports one, two or all (three) messaging patterns so you should be aware of channel characteristics to be able to use it for your aims.

Different transports can be used in WCF.  It is also possible to write a new transport but HTTP, TCP and MSMQ are the most common transports.  These transports have some characteristics so you have to choose a correct transport for your application.  For example, some transports provide better security features.

In addition to the security level that comes with transports, you can use SOAP message security and its authentication or authorization and other security mechanisms to improve security in your application.

It is possible to encode messages in a WCF application.  You can use pure text format or binary format to encode messages.  There are some built-in encoders, but you can write new encoders or extend them.

Reliable Sessions let you to have reliable messaging and it helps you to keep communication alive whenever something is lost.   Reliable Sessions also let you to have sessions.  Sessions keep states on services during several actions.

Interoperability of a channel makes it possible to open doors to other platforms and technologies.  Four types of interoperability are available in WCF.  Basic Profile is very simple and you saw it in traditional web services.  .NET interoperability needs .NET on the client and service, but provides some helpful features such as binary encoding.  MSMQ interoperability is a way to interoperate with other deployments of MSMQ.  The fourth and last interoperability that is very common is WS-*.  This interoperability opens doors for other platforms and can use WS-* protocols.  These protocols provide many important features such as security, transactional operations, reliability and service.


Understanding the structure of services makes up your main knowledge in WCF programming.  Here I want to talk about different concepts around services.

Service Description

Each service is described by its description.  Descriptions show some information about the service.  For example, what it can do and how can you get access to it.

You can use four standards to describe the service:

·         WSDL

·         WS-Policy

·         WS-Metadata Exchange

·         XSD

You may be familiar with some of these standards from the past and traditional web services.


Bindings define the way that a service can communicate with other services and clients.  As we talked in previous sections, each channel has some characteristics and each binding uses a combination of various available properties for a channel.  There are some pre-defined bindings in Windows Communication Foundation, but you can write a custom binding for your own needs.  If you want to be a success in development for WCF, it is essential to have a good understanding about all pre-defined bindings and their properties.  This lets you to choose the correct binding which is an important part of WCF development.

Here is a list of all built-in bindings in WCF.  I strongly recommend you to read this article on MSDN to learn all properties for each binding type.

·         BasicHttpBinding

·         WSHttpBinding

·         WSDualHttpBinding

·         WSFederationHttpBinding

·         NetTcpBinding

·         NetNamedPipeBinding

·         NetMsmqBinding

·         NetPeerTcpBinding

·         MsmqIntergrationBinding

Some of these bindings are very common, like BasicHttpBinding and WSHttpBinding.  Some of these bindings support security mechanisms and are secure.  Some bindings support reliable sessions or duplex messaging.  Some of them support different encodings and contracts.


In WCF contracts define the structure of things.  There are three types of contracts:

·         Service Contract: A service contract defines what it does.  A service can have one or more service contracts.  Service contracts are similar to interfaces in Object Oriented Programming.  You implement an interface for your service logic.  For example, a service that operates as a calculator may have an ICalculator contract with four functions for add, subtract, multiply and divide and implements this interface in Calculator class.

·         Data Contract: Data contracts let you to pass custom objects to service operations.  Using data contracts you define a structure for your objects and pass them to methods.  For example, you may have a Person class that you the Person to be able to pass it to methods.

·         Message Contract: This is less common than previous contract types, but you can use message contracts to change the structure of your messages to use them in your application.  For example, you can change the body of a message.


Endpoints act like wharfs in ports.  They are the access points of a service.  An endpoint associates a service contract and binding with an address to let clients find the service and use it.  Having at least one endpoint is required for a service, but services can have more than one endpoint.

An endpoint contains its elements: Address, Binding, and Service Contract.  A client sends a request to the address then it will be passed to a service contract through a channel which is defined by binding.

There is a correlation between endpoints and addresses.  An address is created based on a few parameters:

·         The transport that is used for communication makes the prefix that will be used (http://, https://, net.tcp:// or …).

·         The place where the service is hosted.  As I said in the introduction article, a service can be hosted in a virtual directory on IIS or can have its own code for hosting.  Format for an address that is hosted on IIS is a bit different than the format for a service that is self-hosted.

·         The port that is used for communication.


The second part of my tutorial about Windows Communication Foundation gave you a description about all fundamental concepts in this technology.  First, we talked about messages, message structure and messaging patterns.  Then we jumped into channels and their main characteristics.  At the end we talked about services and their related concepts such as description, binding, contract and endpoint. In future articles we will spend more time on some of above concepts.

Product Spotlight
Product Spotlight 

©Copyright 1998-2020  |  Page Processed at 2020-07-05 4:06:29 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search