Tuesday, 13 December 2016

WCF binding

Binding describes how a client is going to communicate to WCF service. Binding is used as per client need. It supports different types of protocol to communicate with client and different types of encoding to transfer the data over the internet. So, basically binding is nothing but it is a way of communication between client and service as per client need.

Basic binding
This binding is provided by the BasicHttpBinding class. It is designed to expose a WCF service as an ASMX web service, so that old clients (that are still using an ASMX web service) can consume the new service. By default, it uses the HTTP protocol for transport and encodes the message in UTF-8 text format. You can also use HTTPS with this binding.

Web binding

This binding is provided by the WebHttpBinding class. It is designed to expose WCF services as HTTP requests using HTTP-GET and HTTP-POST. It is used with REST based services that may provide output in XML or JSON format. This is very much used with social networks for implementing a syndication feed.

Web Service (WS) binding:

This binding is provided by the WSHttpBinding class. It is like a basic binding and uses HTTP or HTTPS protocols for transport. But this is designed to offer various WS - * specifications such as WS – Reliable Messaging, WS - Transactions, WS - Security and so on which are not supported by Basic binding.

wsHttpBinding= basicHttpBinding + WS-* specification

WS Dual binding:

This binding is provided by the WsDualHttpBinding class. It is like a WsHttpBinding except it supports bi-directional communication which means both clients and services can send and receive messages.

TCP binding:

This binding is provided by the NetTcpBinding class. It uses TCP protocol for communication between two machines within intranet (means same network). It encodes the message in binary format. This is a faster and more reliable binding compared to the HTTP protocol bindings. It is only used when the communication is WCF-to-WCF which means both client and service should have WCF.

IPC binding:

This binding is provided by the NetNamedPipeBinding class. It uses named pipe for communication between two services on the same machine. This is the most secure and fastest binding among all the bindings.

MSMQ binding:
This binding is provided by the NetMsmqBinding class. It uses MSMQ for transport and offers support to a disconnected message queued. It provides solutions for disconnected scenarios in which the service processes the message at a different time than the client sending the messages.

Federated WS binding:

This binding is provided by the WSFederationHttpBinding class. It is a specialized form of WS binding and provides support to federated security.

Binding describes how client will communicate with service. There are different protocols available for the WCF to communicate to the Client. You can mention the protocol type based on your requirements.

A binding has several characteristics, including the following:
  • Transport - Defines the base protocol to be used like HTTP, Named Pipes, TCP, and MSMQ are some type of protocols.
  • Encoding (Optional) - Three types of encoding are available-Text, Binary, or Message Transmission Optimization Mechanism (MTOM). MTOM is an interoperable message format that allows the effective transmission of attachments or large messages (greater than 64K).
  • Protocol(Optional) - Defines information to be used in the binding such as Security, transaction or reliable messaging capability.

BindingDescription
BasicHttpBindingBasic Web service communication. No security by default.
WSHttpBindingWeb services with WS-* support. Supports transactions.
WSDualHttpBindingWeb services with duplex contract and transaction support.
WSFederationHttpBindingWeb services with federated security. Supports transactions.
MsmqIntegrationBindingCommunication directly with MSMQ applications. Supports transactions.
NetMsmqBindingCommunication between WCF applications by using queuing. Supports transactions.
NetNamedPipeBindingCommunication between WCF applications on same computer. Supports duplex contracts and transactions
NetPeerTcpBindingCommunication between computers across peer-to-peer services. Supports duplex contracts
NetTcpBindingCommunication between WCF applications across computers. Supports duplex contracts and transactions

End Points of WCF

Endpoints provide the configuration required for the communication and create the complete WCF service application.

An Endpoint is a piece of information that tells WCF how to build the runtime communication channels to send and receive messages. An endpoint consists of the three things address, binding and contract. 


All communication with a Windows Communication Foundation (WCF) service occurs through the endpoints of the service. Endpoints provide clients access to the functionality offered by a WCF service.

Each endpoint consists of four properties:
  • An address that indicates where the endpoint can be found.
  • A binding that specifies how a client can communicate with the endpoint.
  • A contract that identifies the operations available.

The Structure of an Endpoint-

Each endpoint consists of the following:
  • A set of behaviors that specify local implementation details of the endpoint.
  • Address: The address uniquely identifies the endpoint and tells potential consumers of the service where it is located. It is represented in the WCF object model by theEndpointAddress class. An EndpointAddress class contains:

    • A Uri property, which represents the address of the service.
    • An Identity property, which represents the security identity of the service and a collection of optional message headers. The optional message headers are used to provide additional and more detailed addressing information to identify or interact with the endpoint.
  • Binding: The binding specifies how to communicate with the endpoint. This includes:

    • The transport protocol to use (for example, TCP or HTTP).
    • The encoding to use for the messages (for example, text or binary).
    • The necessary security requirements (for example, SSL or SOAP message security).
  • Contracts: The contract outlines what functionality the endpoint exposes to the client. A contract specifies:

    • What operations can be called by a client.
    • The form of the message.
    • The type of input parameters or data required to call the operation.
    • What type of processing or response message the client can expect.


Fault Contract in WCF

Fault Contract provides documented view for error accorded in the service to client. This help as to easy identity the what error has occurred, and where. By default when we throw any exception from service, it will not reach the client side. The less the client knows about what happened on the server side, the more dissociated the interaction will be, this phenomenon (not allowing the actual cause of error to reach client) is known as error masking. By default all exceptions thrown on the service side always reach the client as FaultException, as by having all service exceptions indistinguishable from one another, WCF decouples the client from service. 

Syntax

[OperationContract]
[FaultContract(typeof(MyException))]
string getDetails(int value);

Message Contract in WCF

A Message Contract is used to control the structure of a message body and serialization process. It is also used to send / access information in SOAP headers. By default WCF takes care of creating SOAP messages according to service DataContracts and OperationContracts. 

 
A MessageContract controls the structure of a message body and serialization process. A MessageContract can be typed or untyped and are useful in interoperability cases and when there is an existing message format we must comply with. 

When an operation contract required to pass a message as a parameter or return value as a message, the type of this message will be defined as message contract. A message contract defines the elements of the message (like as Message Header, Message Body), as well as the message-related settings, such as the level of message security.

Message contracts give you complete control over the content of the SOAP header, as well as the structure of the SOAP bod

The SOAP header is implemented in the namespace system.web.services.protocol.
  1. [MessageContract]  
  2. public class AutherRequest  
  3. {  
  4.    public string AutherId;  
  5. }  
Message Header

A Message Header is applied to a member of a message contract to declare the member within the message header.
  1. [MessageContract]  
  2. public class AutherRequest  
  3. {  
  4.    [MessageHeader]  
  5.    public string AutherId;  
  6. }  
Message Body Member

A Message Body Member is applied to the member of a message contract to declare the members within the message body.
  1. [MessageContract]  
  2. public class AuthorResponse  
  3. {  
  4.     [MessageBodyMember]  
  5.     public Auther Obj;  
  6. }  

Data Contract in WCF

A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged.

Data contract can be explicit or implicit. Simple type such as int, string etc has an implicit data contract. User defined object are explicit or Complex type, for which you have to define a Data contract using [DataContract] and [DataMember] attribute.

A data contract can be defined as follows:
  • It describes the external format of data passed to and from service operations.
  • It defines the structure and types of data exchanged in service messages.
  • It maps a CLR type to an XML Schema.
  • It defines how data types are serialized and deserialized. Through serialization, you convert an object into a sequence of bytes that can be transmitted over a network. Through deserialization, you reassemble an object from a sequence of bytes that you receive from a calling application.
  • It is a versioning system that allows you to manage changes to structured data.
  1. [DataContract]   
  2. public class CuboidInfo   
  3. {   


Data Contracts define the data type for variables that are the same as get and set properties but the difference is that a Data Contract in WCF is used to serialize and deserialize the complex data. It defines how data types are serialized and deserialized. Using serialization, you can convert an object into a sequence of bytes that can be transmitted over a network. Using de-serialization, you reassemble an object from a sequence of bytes that you receive from a calling application. 

Duplex in WCF

Duplex is a Two-Way Communication Process. In which, a consumer send a message to the service and the service sends a notification that the request processing has been done. It is just like that we send some command about the printing of some papers to the Printer machine. The printer machine start communication by making the connection with our machine, and start printing the paper.
  1. Duplex service allows calling back some operation (function) on the client.
  2. Duplex service also knows as Call Backs. 
  3. All Binding does not support duplex service. 
  4. Duplex communication is not standard. They are absolute Microsoft feature. 
  5. wsDualHttpBinding supports duplex communication over HTTP binding. 
  6. Duplex communication is possible over netTcpBinding and netNamedPipeBinding

MSMQ in WCF

Microsoft Messaging Queue (MSMQ) technology is used for asynchronous communication using messages. MSMQ also can be considered to be an Inter- process communication capability.

Whenever two processes want to communicate with each other in a "Fire and Forget" manner, MSMQ is very useful for that.

Example: For example what if Billing software needs to process 1000 bills at midnight and must send mail to all those users. Such as when the operator runs the software he wants immediate notification. The operator can't wait for all the bills to be processed and gets email.

Here MSMQ plays an important role for communication by billing software to send those 1000's of customer email information into the Queue and the Queue event handler will handle the requests. In this way the Operator gets instant notification of the processes instead of knowing the "Behind the Scene" process.