Search This Blog

2012/11/13

WCF-Window Communication Foundation Getting Started



   
What Is Windows Communication Foundation?
Windows Communication Foundation (WCF) is a framework for building service-oriented applications. In Development phases Microsoft code named WCF as Indigo. Service-oriented applications are applications build using service oriented architecture (SOA) principle
Service-oriented architecture (SOA) is the reliance on Web services to send and receive data. The services have the general advantage of being loosely-coupled instead of hard-coded from one application to another. A loosely-coupled relationship implies that any client created on any platform can connect to any service as long as the essential contracts are met.
Using WCF, one can send data as asynchronous messages from one service endpoint to another  data can be binary data or Word send as XML.WCF service can be hosted in IIS or in an application e.g. a console application or web application.
WCF unifies the programming model provided in .Net Framework; WCF combines features of Web Service, Remoting, MSMQ and COM+. WCF & provides a common platform for all .NET communication.
Where WCF can be used ?
When we need
· 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.

 


Features of WCF


· Service Orientation
              WCF enables us to create service oriented applications.
 · Interoperability
               1) WCF can be used to extend existing legacy code in COM+ rather than having to rewrite it.
2) WCF allows COM developer consume WCF Service using service moniker.
3) .NET Remoting applications integration and migration- .Net Remoting 2.0 and WCF can be side by side, letting us to expose the same business objects over both technologies simultaneously without having to modify our existing .Net Remoting 2.0 code. One can migrate entire service to WCF. Migration from .NET Remoting 2.0 to WCF requires changes to the remote object's interface and its configuration settings.
    Remoting needs client & server both developed on .Net unlike Web service or WCF. In remoting, both applications know about each other. A proxy of an application object is created on the other application.
A recent comparison of WCF & Remoting using performance counters has proven that WCF has an edge over predecessor.
4) Interoperability between ASP.NET Web services & WCF: Interoperability can be achieved by ensuring that services implemented using both technologies conform to the WS-I Basic Profile 1.1 specification. ASP.NET Web services that conform to WS-I Basic Profile 1.1 are interoperable with WCF clients by using WCF system-provided binding BasicHttpBinding
5)Plain Old XML” (POX) applications communicate by exchanging raw HTTP messages that contain only XML application data that is not enclosed within a SOAP envelope.
     WCF can be used to implement services that expose endpoints to clients such as Web browsers and scripting languages that send and receive POX messages. WCF programming model can be used to implement clients that communicate with POX-based services.
· Multiple Message Patterns

             WCF let us to implement services that can exchange messages in several well-known patterns.
Request/Reply pattern: The most common pattern is the request/reply pattern, where one endpoint requests data from a second endpoint. The second endpoint replies.
One-Way Message: A one-way message in which a single endpoint sends a message without any expectation of a reply.
Duplex exchange pattern: In the duplex exchange pattern , two endpoints establish a connection and send data back and forth, similar to an instant messaging program.
· Service Metadata

WCF supports publishing service metadata using formats specified in industry standards such as WSDL, XML Schema and WS-Policy. This metadata can be used to automatically generate and configure clients for accessing WCF services. Metadata can be published over HTTP and HTTPS or using the Web Service Metadata Exchange standard
· Data Contracts
      A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts. A data contract precisely defines, for each parameter or return type, what data is serialized (turned into XML) to be exchanged.
The easiest way to handle data is by creating classes that represent a data entity with properties that belong to the data entity.
    WCF includes a comprehensive system for working with data in this easy manner. Once you have created the classes that represent data, your service automatically generates the metadata that allows clients to comply with the data types you have designed.
  · Security

Messages can be encrypted to protect privacy and you can require users to authenticate themselves before being allowed to receive messages. Security can be implemented using well-known standards such as SSL or WS-SecureConversation.
· Multiple Transports and Encodings

Messages can be sent on any of several built-in transport protocols and encodings. The most common protocol and encoding is to send text encoded SOAP messages using is the HyperText Transfer Protocol (HTTP) for use on the World Wide Web.
      Alternatively, WCF allows us to send messages over TCP, named pipes, or MSMQ. These messages can be encoded as text or using an optimized binary format. Binary data can be sent efficiently using the MTOM standard. If none of the provided transports or encodings suit your needs you can create your own custom transport or encoding.
· Reliable and Queued Messages

WCF supports reliable message exchange using reliable sessions implemented over WS-Reliable Messaging and using MSMQ.
Reliable messaging is how a reliable messaging source (called the source) transfers messages reliably to a reliable messaging destination (called the destination).
Reliable messaging has the following key aspects:
  • Transfer assurances for messages sent from a source to a destination regardless of message transfer failure or transport failures.
  • Separation of the source and the destination from each other, which provides independent failure and recovery of the source and the destination as well as reliable transfer and delivery of messages even though the source or destination is unavailable.
WCF provides the following types of reliable messaging:
1)       Reliable Session: Reliable sessions provide end-to-end reliable transfer of messages between a source and a destination using the WS-ReliableMessaging protocol regardless of the number or type of intermediaries that separate the messaging (source and destination) endpoints. It Includes   transport intermediaries that uses SOAP or Not using SOAP.

The reliable session WCF ensures that messages sent between endpoints are transferred across SOAP or transport intermediaries and are delivered only once and, optionally, in the same order in which they were sent.

It offer reliable transfer without the cost of high latency
2)       Queues in WCF: WCF queued communication is built on top of Message Queuing (also known as MSMQ). WCF queued communication is built on top of Message Queuing (also known as MSMQ).
MSMQ is shipped as an option with Windows that runs as an NT service. It captures messages for transmission in a transmission queue on behalf of the source and delivers it to a target queue. The target queue accepts messages on behalf of the destination for later delivery whenever the destination requests for messages.
     The MSMQ queue managers implement a reliable message-transfer protocol so that messages are not lost in transmission. The protocol can be native or SOAP-based, such as Soap Reliable Messaging Protocol (SRMP).
Unlike reliable sessions, the source and destination do not have to be running at the same time. This implicitly enables scenarios where queues are, in effect, used as a load-leveling mechanism when there is a mismatch between the rate of message production by the source and the rate of the message consumption by the destination
Queues in WCF provide both reliable transfers of messages and separation between sources and destinations at the cost of high latency.

· Durable Messages

A durable message is one that is never lost due to a disruption in the communication. The messages in a durable message pattern are always saved to a database. If a disruption occurs, the database allows you to resume the message exchange when the connection is restored.
· Transactions

WCF also supports transactions using one of three transaction models: WS-AtomicTtransactions, the APIs in the System.Transactions namespace, and Microsoft Distributed Transaction Coordinator
· AJAX and REST Support

REST is an example of an evolving Web 2.0 technology. WCF can be configured to process "plain" XML data that is not wrapped in a SOAP envelope. WCF can also be extended to support specific XML formats, such as ATOM (a popular RSS standard), and even non-XML formats, such as JavaScript Object Notation (JSON).
· Extensibility

The WCF architecture has a number of extensibility points. If extra capability is required, there are a number of entry points that allow you to customize the behavior of a service.


Difference between Web Service in ASP.NET & WCF Service

WCF is a replacement for all earlier web service technologies from Microsoft. One will get a much higher degree of flexibility and portability doing things in WCF than through traditional ASMX, because WCF is designed, from the ground up, to summarize all of the different distributed programming infrastructures offered by Microsoft.
Features
Web Service
WCF
Hosting
It can be hosted in IIS
It can be hosted in IIS, windows activation service, Self-hosting, Windows service
Programming
[WebService] attribute has to be added to the class
[ServiceContraact] attribute has to be added to the class
Model
[WebMethod] attribute represents the method exposed to client
[OperationContract] attribute represents the method exposed to client
Operation
One-way, Request- Response are the different operations supported in web service
One-Way, Request-Response, Duplex are different type of operations supported in WCF
XML
System.Xml.serialization name space is used for serialization
System.Runtime.Serialization namespace is used for serialization
Encoding
XML 1.0, MTOM(Message Transmission Optimization Mechanism), DIME, Custom
XML 1.0, MTOM, Binary, Custom
Transports
Can be accessed through HTTP, TCP, Custom
Can be accessed through HTTP, TCP, Named pipes, MSMQ,P2P, Custom
Protocols
Security
Security, Reliable messaging, Transactions

An endpoint in WCF can be communicated with just as easily over SOAP/XML as it can over TCP/binary and to change this medium is simply a configuration file mod. In theory, this reduces the amount of new code needed when porting or changing business needs, targets, etc.
Web Services can be accessed only over HTTP & it works in stateless environment, where WCF is flexible because its services can be hosted in different types of applications. Common scenarios for hosting WCF services are IIS,WAS, Self-hosting, Managed Windows Service.
The major difference between them is that Web Services Use XmlSerializer. But WCF Uses DataContractSerializer which is better in Performance as compared to XmlSerializer.

Why to prefer DataContractSerializer over XmlSerializer:

  • Only Public fields or Properties of .NET types can be translated into XML
  • Only the classes which implement IEnumerable interface
  • Classes that implement the IDictionary interface, such as Hash table cannot be serialized

Difference between DataContractSerializer and XMLSerializer

  • A practical benefit of the design of the DataContractSerializer is better performance over Xmlserializer.
  • XML Serialization does not indicate which fields or properties of the type are serialized into XML whereas DataCotractSerializer
  • Explicitly shows the which fields or properties are serialized into XML
  • The DataContractSerializer can translate the HashTable into XML

Client Development

Clients for the ASP.NET Web services are generated using the command-line tool WSDL.EXE.
WCF uses the ServiceMetadata tool (svcutil.exe) to generate the client for the service.

Exception Handling

In ASP.NET Web services, unhandled exceptions are returned to the client as SOAP faults.
In WCF Services, unhandled exceptions are not returned to clients as SOAP faults. A configuration setting is provided to have the unhandled exceptions returned to clients for the purpose of debugging.

Performance impact caused by loading the ServiceModel HttpModule

In the .NET Framework 3.0, the WCF HttpModule was installed in the root Web.config file such that every ASP.NET application was WCF enabled. This might affect performance, so one can remove ServiceModel for the Web.config file if not needed as shown in the following example.
<httpModules>
    <remove name=”ServiceModel” />
<httpModules/>

References:



No comments:

Post a Comment