Windows Communication Foundation (WCF)

WCF is a technology or framework to design distributed applications. It helps us to build a general, common, service-oriented application that can be accessed by different device or system. This article is a introduction to Windows Communication Foundation. Summary of the article:

  • What is WCF?
  • Why Do We Need WCF?
  • EndPoint in WCF
  • The ABC of WCF
  • Types of WCF Contracts
  • Difference between Web Service & WCF
  • What are Various Ways of Hosting WCF Services?

What is WCF?
Windows Communication Foundation or WCF is a framework for building service oriented applications. By using WCF we can transfer data as asynchronous message. To developed a Distributed System or Application we are now using different technology for communication like: COM+, .NET Remoting, .NET Enterprise Services, MSMQ, Web services, etc. All these technologies play a different role. To use them we need to develop different solutions for different technologies. We need to focus each communication technology rather than the application business logic.

WCF help us to develop a common, general service oriented programming model for communication. WCF provides a common approach so that the developers can focus on their application rather than on communication protocol.

Example
[ServiceContract]
Public Interface ITest
{
[OperationContract]
String showmessage(string msg)
}

Why Do We Need WCF?
Why we should use WCF or the advantages of WCF:

  • A single WCF system can be accessed by the different machine, system or different communication protocol. No need to developed different WCF for different device or system. For example if we make a WCF service for a particular operations, then it can be used in web, desktop, Console, Mobile applications.
  • It can be hosted in IIS, windows activation service, Self-hosting, Windows service.
  • It can maintain transaction like COM+ Does
  • It can maintain state
  • It can control concurrency
  • It can be hosted on IIS, WAS, Self hosting, Windows services
  • It has AJAX Integration and JSON (JavaScript object notation) support
  • It use DataContractSerializer which is better in Performance as compared to XmlSerializer

EndPoint in WCF
In application all types of communication with the WCF will occur via the endpoints. It specifies a Contract that defines which methods of the Service class that will be accessible via the endpoint. Each endpoint may represent a different set of methods. It also defines a binding that specifies how a client will communicate with the service and the address where the endpoint is hosted.

The ABC of WCF
The WCF mantra is ABC.  ABC is the key to understanding how a WCF service endpoint is composed. A short descriptions regarding this is given bellow:
A (Address)
A stands for Address(Where?): Where is the service? Specifies the location of the service which will be like http://Myserver/MyService.Clients
B (Binding)
B stands for Binding(How?): How do I talk to the service? Specifies how the two parties will communicate in terms of transport and encoding and protocols.
C (Contract)
C stands for Contract(What?): What can the service do for me? Specifies the interface between client and the server.

Types of WCF Contracts
WCF has three contracts and they are given below:

  1. Service Contract
  2. Operation Contract
  3. Data Contract

Service Contract
Defines the methods of the service.
[ServiceContract]
public interface IOperation
{
[OperationContract]
double Add(double i, double j);
[OperationContract]
double Sub(double i, double j);
}

Operation Contract
Defines the method.
[OperationContract]
double Add(double i, double j);

Data Contract
Defines the data types used by the service methods.
[DataContract]
public class ComplexNumber
{
private int RealNumber;
private int ImaginaryNumber;

[DataMember]
public int RealNumber { get; set; }

[DataMember]
public int ImaginaryNumber { get; set; }

Difference between Web Service & WCF
A comparison between WCF & web service is given bellow:
Web Service

  1. It use HTTP
  2. It is only used in web application
  3. Unhandled exceptions are return to client as SOAP fault
  4. It use XmlSerializer
  5. Accessed through HTTP
  6. It can be hosted in IIS
  7. [WebService] attribute has to be added to the class
  8. [WebMethod] attribute represents the method to client

WCF

  1. It use HTTP, TCP IP
  2. It can be used in web, desktop, Console, Mobile applications
  3. Unhandled exceptions are not return as SOAP fault. Configuration setting is provided
  4. It use DataContractSerializer which is better in Performance as compared to XmlSerializer
  5. Accessed through HTTP, TCP, MSMQ, P2P
  6. It can be hosted in IIS, windows activation service, Self-hosting, Windows service
  7. [ServiceContract] attribute has to be added to the class
  8. [OperationContract] attribute represents the method to client

What are Various Ways of Hosting WCF Services?
We can host a WCF Service in three major ways:

  1. IIS – Host in application domain or process provided by IIS Server
  2. Windows Activation Service (WAS) – Host in application domain and process provided by WAS
  3. Self-hosting – Hosting in its own application domain

That’s all about wcf.

Comments

  1. By Jon Smith

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *