WCF Services - Choosing the Appropriate WCF Binding
In this article, we will remain focused on learning about the existing bindings available in WCF.
WCF is a programming model that enables us to develop and then expose our services in a variety of different ways. It means a single WCF service can be exposed with multiple wire formats and message protocols. In WCF bindings define how to communicate with the service. So, binding specifies the communication protocol as well as the encoding method that will be used. Optionally, it can specify other important factors like transactions, reliable sessions and security.
WCF includes a number of built-in bindings that we can use to expose our service, but WCF is extensible so we can define our own custom bindings to fulfill specific needs.
But instead of explaining those bindings in general, we will try to understand these bindings with respect to some suitable scenarios in which it can be used. For understanding WCF Binding internals, another WCF article of mine has these details.
If we are going to develop a WCF service that will be consumed by non-WCF client applications, then we can expose our service using BasicHttpBinding or WsHttpBinding. So, how these two bindings differ from each other is explained as:
- 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.
- 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.
- WsDualHttpBinding has all the features of WsHttpBinding with the addition that it supports Duplex MEP (Message Exchange Pattern). In this MEP, the service can communicate with the client via a callback. Its basically a two-way communication.
Note: For further details on Message Exchange Patterns, you can refer to my other article "WCF-Top 10 Interview Questions" which explains MEPs in details.
- WsFederationHttpBinding is a specialized form of WS Binding that offers support for federated security.â€ƒ
Single Computer Scenario: If our WCF service resides on a single computer, then netNamedPipeBinding will be the best choice.
- NetNamedPipeBinding is a secure and reliable binding on a single WCF computer for inter-process communication (IPC). It provides support for binary encoding which is the best choice in this scenario and uses named pipes as the transport for SOAP messages.
Intranet/Cross Computers .NET Communication Scenario: If we need to communicate across computers with the same .NET technology on the intranet, then the netTcpBinding or netPeerTcpBinding options are available. It's basically the replacement or enhancement of earlier .NET Remoting technology.
- NetTcpBinding supports reliability, transactions and security. It also supports the TCP protocol and binary as the encoding method. We can say that it's the most optimized or fastest binding because both the client and the service are on the same WCF technology.
- NetPeerTcpBinding supports features as that of the netTcpBinding but it provides secure binding for a peer-to-peer environment with WCF Services.
Disconnected Queued Scenario:
- NetMsmqBinding is required in a cross-machine environment with secure and reliable queued communication. This uses MSMQ as the transport.