A Beginner's Tutorial For Understanding WCF

Windows Communication Foundation (WCF)

 
Windows Communication Foundation (WCF) is a framework for building service-oriented applications by which we can send asynchronous message/data from one service endpoint to another service endpoint.
 
WCF combines the features of all the distributed technologies, such as:
  1. COM+ Services
  2. .NET Remoting
  3. Web Services

Difference between WCF and Web Services

 
Web Services WCF
It is available in the namespace "System.Web.Services.WebService" class It is available in the namespace "System.ServiceModel" class
It is only supports IIS hosting It supports various hosting like IIS, Self Hosting (Console hosting), Windows Activation Services and Windows Services
It supports one-way communication and Request-Response It supports one-way and two-way (duplex) communication and Request-Response
Protocol security is supported Protocol security, transaction and reliable message is supported
Web Services are slower than WCF WCF is faster than Web Services
System.Xml.serialization name space is used for serialization System.Runtime.Serialization namespace is used for serialization
Transport protocol like HTTP, TCP and custom is supported Transport protocol like HTTP, TCP, Named Pipes, MSMQ, P2P and custom is supported
 

Fundamentals of WCF

 
To understand the architecture of WCF, you should be aware of the fundamentals of WCF. The fundamentals are as follows:
  • Unification: WCF enhances the development of distributed applications and increases the productivity of developer by unifying the following technologies:

    • COM+ Services: COM+ provides the infrastructure to create and deploy distributed applications. It provides various services, such as object pooling, just in time activation and synchronization. These services allow you to create highly secure and scalable applications.
    • Web Services: Web services are a program component that allow you to develop scalable, loosely coupled, platform-independent applications.
    • .NET Remoting: .NET Remoting enables communication among multiple objects in differing application domains or processes by using transportation protocols such as HTTP and TCP.
    • Microsoft Message Queuing: It enables a .NET application to send messages even when the destination is not reachable and are stored in a queue until the destination application becomes available. The messages are delivered to the application once the destination application becomes available. This prevents loss of data due to the unavailability of an application.
  • Interoperability: WCF enables two applications built on the same or different platforms to communicate with each other within and across a network.
  • Service Orientation: WCF provides a highly productive programming model for building distributed systems that involve developing loosely-coupled services. These services can be independently managed, updated and deployed.
 
Figure 1: WCF Fundamentals
 

WCF Architecture

 
There are four major layers that provide developers with a new service-oriented programming model. The WCF architecture consists of the following layers.
 
Let us briefly learn about them.
 
 
Figure 2: WCF Architecture
  • Contracts (Layer 1): The contract layer contains various types of contracts and policy and binding used in WCF. The various types of contracts present in the contract layer are as follows:

    • Service Contract: Service contract includes the operations performed by the service and exposes them as a single unit through an interface.
      1. [ServiceContract]     
      2. public interface IService    
      3. {     
      4.    // TODO: Add your service operations here     
      5. }  
    • Data Contract:: Exposes the user defined data types and classes in a WCF service.
      1. [DataContract]     
      2. public class Employee     
      3. {     
      4.    [DataMember]     
      5.    public int emp_id;    
      6.    [DataMember]     
      7.    public int emp_name;     
      8. }
    • Message Contract: The message contract describes the format and structure of the message exchanged between a client application and a WCF service.
      1. [MessageContract]     
      2. public class StoreMessage     
      3. {     
      4.    [MessageHeader] public DateTime CurrentTime;     
      5.    [MessageBodyMember] public Employee emp_id;     
      6. }
    • Fault Contract: Fault contract enables you to send a customized error message to a client by creating a user-defined class.
      1. [ServiceContract]     
      2. public interface IService     
      3. {     
      4.    [OperationContract]     
      5.    [FaultContract(typeof(SampleFaultException))]     
      6.    DataSet GetDetails(int Employee_ID);    
      7. }     
      8. [DataContract]     
      9. public class SampleFaultException    
      10. {     
      11.    [DataMember]     
      12.    public string errorMessage;     
      13. }   
    • Operation Contract: Operation contract is used to expose the operations that a service can perform. It defines the methods of a WCF service along with the parameters and return type of the methods.
      1. [ServiceContract]    
      2. public interface IService    
      3. {     
      4.    [OperationContract]     
      5.    DataSet GetDetails(int Employee_ID);     
      6. }  
    • Policies and Binding: It specifies the conditions required to communicate with a service. For example, security, protocol and the encoding used for binding.
  • Service Runtime (Layer 2): The service runtime layer contains the behavior of the service that occurs during the execution of the service. The following are the list of various behaviors managed by the service runtime layer:

    • Throttling behavior: This behavior controls the number of processed messages.
    • Error behavior: This behavior determines what will happen when an error occurs during the service runtime.
    • Metadata behavior: This behavior governs how and whether the metadata of the service is made available to the clients.
    • Instance behavior: This behavior specifies the number of instances of the services needed to process the requests from the client.
    • Message inspection: This behavior inspects the parts of a message. With this behavior we can check the format of the parameters of a message.
    • Transaction behavior: This behavior is responsible for rolling back the transaction, if a process fails during the service runtime.
    • Dispatch behavior: This behavior is responsible for controlling how a message is processed by the WCF infrastructure.
    • Concurrency behavior: This behavior is a measure of how many threads can access a given instance of a service.
    • Parameter filtering: This behavior allows predetermined actions to be done based on message header filters.

      Note: Behaviors are basically aspects of how the service handles a call once it comes into the service.
  • Messaging (Layer 3): Using the channels, the messaging layer processes the message that is processed and transported to a client accessing the service. The two types of channels are as follows:

    • Transport channels: This channel reads and writes messages to and from a client using an encoder to convert a message in a standard format. The examples of the transport channels are XML, HTTP, named pipes, TCP and MSMQ.
    • Protocol channels: This channel processes a message by reading and writing additional headers to the message. The examples of protocol channels are WS-Security and WS-Reliability.
  • Activation and Hosting (Layer 4): This layer supports the execution of services in various environments, such as Windows Services, IIS and Windows Activation Services (WAS). A service can either be self-hosted or hosted in the context of another application. Let us briefly learn about them.
    • Self-Hosting: A WCF service can be self-hosted as a console application or WPF application or within Windows Forms with a graphical UI. This hosting is very flexible and easy to use, but suitable only during development phases of a distributed application.
    • IIS: When a WCF service is hosted in IIS the client can access the service over the internet. When a service is hosted in IIS, it acquires the benefits of IIS such as process lifetime management and automatic update after configuration changes.
    • Windows Services: We can host a WCF service as a Windows service on a client's computer and is automatically stopped when the computer shuts down.
    • Windows Activation Services (WAS): WAS is a service available on Windows that enables the WCF application to run automatically when the computer is started. WAS works with HTTP, TCP, Named pipes and MSMQ protocols. This enables the WCF service to turn on the internet without IIS support.

Programming Model of WCF

 
All the communication with a WCF service occurs through the endpoints of the service. An endpoint of a WCF service acts as a gateway for communication with other applications. It is the location where messages are sent or received and contains all the information required for the exchange of a message.
 
An endpoint is composed of an address, a binding and a contract known as the ABCs of endpoints as shown in the figure.
 
 
Figure 3: The ABCs of Endpoints
  • Address: In the ABCs of WCF, "A" stands for Address that specifies where the service is. Every service has a unique address whose format is as follows:
    [Transport] ://[Domain Name]:[Port]//[Service Name]
  • Binding: In the ABCs of WCF, "B" stands for Binding that specifies how to talk with the service. WCF has a couple of builtin bindings to fulfill some specific needs and we can also define our own custom binding. All builtin bindings are defined in the System.ServiceModel namespace.

    The following are the various types of bindings supported by WCF:
    • BasicHttpBinding: This is the basic web service for the communicate with a web server or a client application. It is designed to expose a WCF service as an asmx web service. This binding uses the HTTP protocol for communication.
    • NetMsmqBinding: It is used to implement message queuing in a WCF service. It enables a client application to send a message to a WCF service even if the service is unavailable.
    • NetTcpBinding: It allows the communication between a WCF service and a .NET client application over a network. It uses the TCP protocol and is a much faster and more reliable binding compared to HTTP protocol binding.
    • NetPeerTcpBinding: For peer-to-peer communication where messages are sent and received using the TCP protocol.
    • WSHttpBinding: It is similar to BasicHttpBinding and uses the HTTP or HTTPS protocol. It also supports WS-Transactions and WS-Security that are not supported by the BasicHttpBinding.
    • WSDualHttpBinding: It is similar to WSHttpBinding, except it supports bi-directional communication which means both clients and services can communicate with each other by sending and receiving messages.
    • NetNamedPipeBinding: This binding uses named pipes for communication between two services on the same machine that is most secure and the fastest binding among all the bindings.
  • Contract: In the ABCs of WCF, "C" stands for Contract that tells us about what can service do for me.

Advantages of WCF

  • WCF supports various protocols.
  • Security over transport as well as Message Level.
  • Easy to implement MSMQ and REST Service.
  • It can be hosted on IIS, WAS, Self hosting or Windows Services.
  • Multiple message patterns.
  • WCF supports better exception handling using a Fault Contract.

Summary

 
Using this article we have learned the basics of WCF, the WCF architecture and the ABCs of endpoints of WCF for beginners. I hope you liked this article. Thanks for reading.
Coming up Next: Creating and Consuming WCF Service
 
Until then Happy Coding!


Similar Articles