Introduction to WCF Services

Windows Communication Foundation (WCF)

WCF is used to communicate with cross-platform applications and to develop network distributed applications.

Previously WCF was known as Indigo service and later on with .NET Framework it became WCF. WCF was first introduced with .NET Framework 3.0 in 2006. Later on it became the most popular programming language for network distributed application because it provides all the combined features of Web Service, Remoting, MSMQ and COM+ on a single platform without using a different service for different requirements.

Let us understand WCF with the following diagram.


Figure 1: WCF Technology

From the preceding diagram, I will try to provide you the message that WCF is the common technology to provide the features of all technologies being used to communicate for various purposes.

Why WCF

WCF provides a wide range of features, including communication protocol support, security and so on. Let us consider the following example:


Figure 2: Web Service Communication

This is the normal communication of Web Service and client in which the client requirement is to exchange the data using the HTTP Protocol.

In the preceding example the Web Service communicates with the client using the HTTP protocol and only the HTTP protocol supports web services. Suppose in the future the client wants to exchange data using TCP as well as HTTP without creating a separate service then this is not possible with a Web Service. This can however be done using a WCF service.

 
Figure 3: Why WCF
 
From the preceding diagram I will try to explain that a WCF service can exchange data or messages in any format including SOAP and binary using a wide range of communication protocols such as HTTP, TCP and so on.

WCF Architecture

The WCF architecture consists of the following components:

  • Contracts.
  • Service Run time.
  • Messaging.
  • Activation and Hosting .

Let us briefly learn about them.

Contracts in WCF

Contracts are useful to build the WCF service applications. Contracts define the protocol (binding) the service uses, how the communication will be done, what message exchange format to use and others.

The following are the Contracts available in WCF:

  • Service Contracts
  • Data Contracts
  • Message Contracts
  • Fault Contract
  • Operation Contract
  • Policies and Binding

Service contracts

Service contracts define the interface for the service. It can be defined as follows.

Syntax

  1. [ServiceContract]     
  2. public interface IService1   
  3.   {          
  4. // TODO: Add your service operations here    
  5.  } 

Operation contract

The operation contract defines the method that is exposed to the client for exchanging the information between client and server. The operation contract describes the functionality to be given to the client such as Addition, Subtraction and others.

It can be defined as in the following code.

  1. public interface IService1  
  2.     {  
  3.         [OperationContract]  
  4.         string GetData(int value);  
  5.   
  6.         [OperationContract]  
  7.         CompositeType GetDataUsingDataContract(CompositeType composite);  
  8.     
  9.     } 

Data Contract

Data Contracts are the same as get set properties but the difference is that a data contract in WCF serializes and deserializes the complex data. It defines how data types are serialized and deserialized. Using serialization, you convert an object into a sequence of bytes that can be transmitted over a network. Using deserialization, you reassemble an object from a sequence of bytes that you receive from a calling application.

It can be defined as follows:

  1. [DataContract]  
  2.     public class Student  
  3.     {  
  4.         private string _Name;  
  5.   
  6.         private string _City;  
  7.   
  8.   
  9.         [DataMember]  
  10.         public string Name  
  11.         {  
  12.             get { return _Name; }  
  13.             set { _Name = value; }  
  14.         }  

Fault Contract

A Fault contract is used to handle the exception and understand the cause of the error that occurs in the WCF service. When we develop a managed application or service, we handle the exception using a try- catch block, but this exception handling is technology-specific.

The following is the syntax to raise a custom error in WCF:

  1. [ServiceContract]  
  2.     public interface IGetDetailsService  
  3.     {  
  4.         [OperationContract]  
  5.         [FaultContract(typeof(Student))]  
  6.         Student GetDetails(string Name);  
  7.     }  
  8.   
  9.     [DataContract]  
  10.     public class Student  
  11.     {  
  12.         private string _Name;  
  13.   
  14.         private string _City;  
  15.   
  16.         [DataMember]  
  17.         public string Name  
  18.         {  
  19.             get { return _Name; }  
  20.             set { _Name = value; }  
  21.         }  
  22.   
  23.         [DataMember]  
  24.         public string City  
  25.         {  
  26.             get { return _City; }  
  27.             set { _City = value; }  
  28.          }  

Message contracts

The default SOAP message format is provided by the WCF run time for communication between a client and service. If it is not meeting your requirements then we can create our own message format. This can be done by using the Message Contract attribute.

It can be defined as:

  1. [MessageContract]  
  2. public class Person  
  3. {  
  4.   [MessageHeader] public Operation Name;  
  5.   [MessageHeader] public string city;  
  6.   [MessageBodyMember] private Home Address;  
  7.   [MessageBodyMember] private Home Streat;  
  8.   [MessageBodyMember] public int age;  

Policies and Binding in WCF

Specify conditions required to communicate with a service. For example, the security requirements to communicate with the service, protocol and the encoding used for binding.

Service Run-time in WCF

It contains the behavior that occurs during the execution of the service.The following are the behaviors managed by a service run-time layer.

  1. Throttling Behavior
  2. Error Behavior
  3. Metadata Behavior
  4. Instance Behavior
  5. Transaction Behavior
  6. Dispatch Behavior
  7. Concurrency Behavior
  8. Parameter filtering
  9. Message inspection
  • Throttling Behavior: Controls how many messages are processed.
  • Error Behavior: Specifies what occurs, when internal error occurs on the service.
  • Metadata Behavior: Tells how and whether metadata is available to outside world.
  • Instance Behavior: Specifies how many instances of the service must be created while running.
  • Transaction Behavior: Enables the rollback of transacted operations if a failure occurs.
  • Dispatch Behavior: Controls how a message is processed by the WCF Infrastructure.
  • Concurrency Behavior: Controls how many threads can access a given instance of service.
  • Parameter filtering: It filters the message headers and executes preset actions based on the filters of the message headers.
  • Message inspection: Inspects a specific part or all parts of the message through the service.

Messaging in WCF Service

The Messaging Layer is composed of channels. A channel is a component that processes a message in some way; for example, by authenticating a message. A set of channels is also known as a channel stack. Channels are the core abstraction for sending messages to and receiving messages from an endpoint. Broadly we can categorize channels as:

  1. Transport Channels
  2. Protocol Channels

Transport Channels

Handles sending and receiving messages from a network. Protocols like HTTP, TCP, named pipe and MSMQ.

Protocol Channels

Implements a SOAP=based protocol by processing and possibly modifying messages. For example, WS-Security and WS-Reliability.

Activation and Hosting in WCF

Services can be hosted or executed by the following mechanisms.

IIS

Internet information Service (IIS) provides a number of advantages if a Service uses HTTP as the protocol. It does not require host code to activate the service, it automatically activates service code.

Windows Activation Service (WAS)

Windows Activation Service (WAS) is the new process activation mechanism that ships with IIS 7.0. In addition to HTTP-based communication, WCF can also use WAS to provide message-based activation over other protocols, such as TCP and named pipes.

Self-Hosting

A WCF service can be self-hosted as a console application or Windows Forms or WPF application with a graphical UI.

Windows Service

WCF can also be hosted as a Windows Service, so that it is under control of the Service Control Manager (SCM).

Advantages of WCF

  1. Support for various protocols.
  2. Security over transport as well as Message Level.
  3. Easy to implement MSMQ and REST Service.
  4. Load balancing and support scaling.
  5. It can control concurrency.
  6. It can be hosted on IIS, WAS, Self hosting or Windows Services.
  7. Multiple message patterns.
  8. Unhandled exceptions are not returned to the client as SOAP faults. WCF supports better exception handling using a Fault Contract.

Note

Please refer to the following articles of minie for a complete understanding:

Summary

I hope this article is useful for all students and beginners. Please contact me if you have any suggestions related to this article.

Up Next
    Ebook Download
    View all
    Learn
    View all