Friday 20 June 2014

WCF Introduction

What is WCF?

Microsoft refers WCF as a programming platform that is used to build Service-oriented applications. Windows Communication Foundation is basically a unified programming model for developing, configuring and deploying distributed services. Microsoft has unified all its existing distributed application technologies (e.g. MS Enterprise Services, ASMX web services, MSMQ, .NET Remoting etc) at one platform i.e. WCF. Code name for WCF was Indigo.

Why to use WCF? or What are the advantages for using WCF?

  • Service Orientation is one of the key advantages of WCF. We can easily build service-oriented applications using WCF.
  • If compared with ASMX web services, WCF service provides reliability and security with simplicity.
  • As oppose to .NET Remoting, WCF services are interoperable.
  • Different clients can interact with same service using different communication mechanism. This is achieved by using service endpoints. A single WCF service can have multiple endpoints. So, developer will write code for service once and just by changing configuration (defining another service endpoint), it will be available for other clients as well.
  • Extensibility is another key advantage of WCF.  We can easily customize a service behavior if required.

What are the core components of WCF Service?


A WCF service has at least following core components.
  • Service Class:  A ervice class implementing in any CLR-based language and expose at least one method.
  • Hosting Environment: a managed process for running service.
  • Endpoint: a client uses it to communicate with service.

What is the difference between WCF and ASMX Web services?

The basic difference is that ASMX web service is designed to send and receive messages using SOAP over HTTP only. While WCF service can exchange messages using any format (SOAP is default) over any transport protocol (HTTP, TCP/IP, MSMQ, Named Pipes etc).

What are the Endpoints in WCF? or Explain ABCs of endpoint?

For WCF services to be consumed, it’s necessary that it must be exposed; Clients need information about service to communicate with it. This is where service endpoints play their role.
A service endpoint has three basic elements or also called ABCs of an endpoint i.e. Address, Binding and Contract.
Address: It defines “WHERE”. Address is the URL that identifies the location of the service.
Binding: It defines “HOW”. Binding defines how the service can be accessed.
Contract: It defines “WHAT”. Contract identifies what is exposed by the service.

What is a WCF Binding? How many different types of bindings available in WCF?


Bindings in WCF actually defines that how to communicate with the service. Binding specifies that what communication protocol as well as encoding method will be used. Optionally, binding can specify other important factors like transactions, reliable sessions and security.
There are different built-in bindings available in WCF, each designed to fulfill some specific need.
  • basicHttpBinding
  • wsHttpBinding
  • netNamedPipeBinding
  • netTcpBinding
  • netPeerTcpBinding
  • netmsmqBinding

1)    BasicHttpBinding is designed to replace ASMX Web services. It supports both HTTP and Secure HTTP. As far as encoding is concerned, it provides support for Text as well as MTOM encoding methods. BasicHttpBinding doesn’t support WS-* standards like WS-Addressing, WS-Security and WS-ReliableMessaging.
2)    WsHttpBinding also supports interoperability. With this binding, the SOAP message is, by default, encrypted. It supports HTTP and HTTPS. In terms of encoding, it provides support for Text as well as MTOM encoding methods. It supports WS-* standards like WS-Addressing, WS-Security and WS-ReliableMessaging. By default, reliable sessions are disabled because it can cause a bit of performance overhead.
3)    WsDualHttpBinding has all features of WsHttpBinding with addition that it supports Duplex MEP (Message Exchange Pattern). In this MEP, service can communicate with client via callback. Its basically a two way communication.
4)    WsFederationHttpBinding is a specialized form of WS Binding that offers support for federated security.If our WCF service resides on a single computer, then netNamedPipeBinding will be the best choice.
5)    NetNamedPipeBinding is secure and reliable binding on a single WCF computer across process communication. It provides support for binary encoding which is the best choice in this scenario and uses named pipes as transport for SOAP messages.
Intranet/Cross Computers .NET Communication Scenario:
If we need to communicate across computers with same .NET technology on intranet, then netTcpBinding or netPeerTcpBinding options are available. It’s basically the replacement or enhancement of earlier .NET Remoting technology.
6)    NetTcpBinding supports reliability, transactions and security. It also supports TCP protocol and binary as encoding method. We can say that it’s the most optimized or fastest binding because both client and service are on the same WCF technology.
7)    NetPeerTcpBinding supports features as that of netTcpBinding but it provides secure binding for peer-to-peer environment with WCF Services.
Disconnected Queued Scenario:
8)    NetMsmqBinding is required in a cross machine environment with secure and reliable queued communication.


Can we have multiple endpoints for different binding types in order to serve different types of clients?

Yes, we can have multiple endpoints for different binding types. For example, an endpoint with wsHttpBinding and another one with netTcpBinging.

What are the hosting options for WCF Services? Explain.

For a service to host, we need at least a managed process, a ServiceHost instance and an Endpoint configured. Possible approaches for hosting a service are:
1.    Hosting in a Managed Application/ Self Hosting
a.    Console Application
b.    Windows Application
c.    Windows Service
2.    Hosting on Web Server
a.    IIS 6.0 (ASP.NET Application supports only HTTP)
b.    Windows Process Activation Service (WAS) i.e. IIS 7.0 supports HTTP, TCP,
NamedPipes, MSMQ.

What are Contracts in WCF?


A Contract is basically an agreement between the two parties i.e. Service and Client. In WCF, Contracts can be categorized as behavioral or structural.
  1. Behavioral Contracts define that whatoperationsclientcanperform on a service.
  • ServiceContract attribute is used to mark a type as Service contract that contains operations.
  • OperationContract attributes is used to mark the operations that will be exposed.
  • Fault Contract defines what errors are raised by the service being exposed.
  1. Structural Contracts
  • DataContract  attribute define types that will be moved between the parties.
  • MessageContract attribute define the structure of SOAP message.


What Message Exchange Patterns supported by WCF?

  •  Request/Response
  •  One Way
  •  Duplex
Request/ResponseIt’s the default pattern. In this pattern, a response message will always be generated to consumer when the operation is called, even with the void return type. In this scenario, response will have empty SOAP body.

One WayIn some cases, we are interested to send a message to service in order to execute certain business functionality but not interested in receiving anything back. OneWay MEP will work in such scenarios.If we want queued message delivery, OneWay is the only available option.

DuplexThe Duplex MEP is basically a two-way message channel. In some cases, we want to send a message to service to initiate some longer-running processing and require a notification back from service in order to confirm that the requested process has been completed.