Thursday, June 16, 2011

Windows Communication Foundation (WCF)

INTRODUCTION:
Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application. An endpoint can be a client of a service that requests data from a service endpoint. The messages can be as simple as a single character or word sent as XML, or as complex as a stream of binary data. A few sample scenarios include:

* A secure service to process business transactions.
* A service that supplies current data to others, such as a traffic report or other monitoring service.
* A chat service that allows two people to communicate or exchange data in real time.
* A dashboard application that polls one or more services for data and presents it in a logical presentation.
* Exposing a workflow implemented using Windows Workflow Foundation as a WCF service.
* A Silverlight application to poll a service for the latest data feeds.

While creating such applications was possible prior to the existence of WCF, WCF makes the development of endpoints easier than ever. In summary, WCF is designed to offer a manageable approach to creating Web services and Web service clients.

WHY WCF?

In this WCF tutorial we introduce the primary reasons for moving from other technologies to WCF as well as how to get started using WCF. Prior to .Net 3.0 it was not an easy matter to select a particular technology for communicating between systems due to the number of technologies available from Microsoft. For example, users could have used Web Service to communicate between a Java based application and a .Net application; WSE users could have take advantage of some of the WS-* message protocols; MSMQ has the ability to queue messages which helps to communicate between intermittently connected solutions; Enterprise services (the successor of COM+) helps to build distributed application easily; .Net Remoting is a fast way to move messages between two .NET applications. All the above mentioned technologies have their pros and cons. Using WCF now we can take the advantage of all the above distributed technologies in a unified manner and WCF is the successor to all these message distribution technologies.

WCF Model
A WCF service is made up of three parts: the service, one or more endpoints and a hosting environment.
A service is basically a class written in a .Net compliant language which contains some methods that are exposed through the WCF service. A service may have one or more endpoints – an endpoint is responsible for communication from the service to the client.
Endpoints also have three parts which are known as ‘ABC’: ‘A’ for Address, ‘B’ for Binding and ‘C’ for Contracts.
Address: Address specifies the info about where to find the service.
Binding: Binding specifies the info for how to interact with the service.
Contracts: Contracts specifies the info for how the service is implemented and what it offers.
Finally a hosting environment where the service is contained.
WCF bindings: System-provided WCF bindings are used to specify the transport protocols, encoding, and security details required for clients and services to communicate with each other. As per MSDN followings are the system-provided WCF
bindings:
BasicHttpBinding: A binding that is suitable for communication with WS-Basic Profile conformant Web Services like ASMX-based services. This binding uses HTTP as the transport and Text/XML as the message encoding.
WSHttpBinding:
A secure and interoperable binding that is suitable for non-duplex service contracts.
WSDualHttpBinding: A secure and interoperable binding that is suitable for duplex service contracts or communication through SOAP intermediaries.
WSFederationHttpBinding: A secure and interoperable binding that supports the WS-Federation protocol, enabling organizations that are in a federation to efficiently authenticate and authorize users.
NetTcpBinding:
A secure and optimized binding suitable for cross-machine communication between WCF applications
NetNamedPipeBinding:
A secure, reliable, optimized binding that is suitable for on-machine communication between WCF applications.
NetMsmqBinding:
A queued binding that is suitable for cross-machine communication between WCF applications.
NetPeerTcpBinding :
A binding that enables secure, multi-machine communication.
MsmqIntegrationBinding :
MsmqIntegrationBinding: A binding that is suitable for cross-machine communication between a WCF application and existing MSMQ applications.

Distributed:
With the advent of WCF, communication between applications and systems has become much easier. WCF is a means to build distributed applications in a Microsoft environment, clients however may or may not use any Microsoft technology in order to consume WCF services.

Hosting of WCF Service
WCF service cannot exist in Void.
Every WCF service must be hosted in a Windows process called the Host process.
IIS Hosting
Main advantage of hosting a service in IIS is Host process is launched automatically upon the first client request.
Disadvantage is that only HTTP protocol could be used.
Another disadvantage is all the service will run on the same port.
Here Host Process life cycle is managed by the IIS.
Self Hosting
This requires adding a managed code to host the process.
The code could be either a Windows form application or Console application.
Here host process must be running before client makes a call to the service.
Mainly Console applications are used for the purpose of self hosting.
The Console/Windows application must specifically create and open an instance of ServiceHost object.
The ServiceHost then remains open and available until it is no longer needed.
Configuration about the Service is being kept in the App.Config file.

No comments:

Post a Comment