When to use which bindings in WCF

发布时间 2023-05-05 17:46:04作者: ChuckLu

When to use which bindings in WCF

WCF supports several different bindings, and the choice of which binding to use depends on the requirements of the service and the client. Here is a brief overview of some of the most common bindings and when to use them:

  1. BasicHttpBinding: This binding is appropriate for interoperability with non-WCF clients and other SOAP web services. It uses HTTP or HTTPS as the transport and SOAP 1.1 or SOAP 1.2 as the message encoding.

  2. WsHttpBinding: This binding supports more advanced features like reliable messaging and security. It uses HTTP or HTTPS as the transport and SOAP 1.2 as the message encoding. It's suitable for scenarios where message security and session management are required.

  3. NetTcpBinding: This binding is optimized for performance and is suitable for use in intranet environments where both the service and client are on the same network. It uses TCP/IP as the transport and binary message encoding.

  4. NetNamedPipeBinding: This binding is similar to the NetTcpBinding, but it uses named pipes instead of TCP/IP. It's suitable for use in intranet environments where both the service and client are on the same machine.

  5. WebHttpBinding: This binding is used for building RESTful services. It uses HTTP or HTTPS as the transport and JSON or XML as the message encoding.

  6. DuplexBinding: This binding is used for building duplex services, where the service can send messages back to the client without being prompted. It uses a combination of transport, message encoding, and session management protocols to achieve this.

The choice of which binding to use depends on the specific requirements of the service and the client, such as the network environment, security requirements, and messaging patterns.

 

WCF comes with the number of built-in bindings and this article describes the pros and cons of each of these bindings. It will help you to choose appropriate bindings depending on your requirements and create endpoints for communicating with clients.

What is WCF Bindings?

WCF bindings decide how the Windows Communication Foundation service endpoint should communicate with clients. Each WCF service endpoint requires binding to be specified, if not set explicitly it uses default basicHttpBinding. It gives communication details and defines below things.

  1. Protocols: specifies what type of security can be used like a reliable messaging or transaction context.
  2. Encoding: specifies the type of message encoding like binary or text.
  3. Transport: specifies communication transport to be used like HTTP or TCP.

 

WCF Predefined Bindings

  1. basicHttpBinding

    basicHttpBinding is best when you have traditional ASMX(Active Server Methods) web services and needs to be replaced with WCF. It supports text as well as MTOM encodings and it does not support WS-* standards like WS-Addressing, WS-Security, and WS-ReliableMessaging.

    basicHttpBinding uses HTTP or HTTPS protocols. You can configure SSL for Transport Layer security with basicHttpBinding.

  2. WsHttpBinding

    This is secure and interoperable bindings use SOAP over HTTP. With WsHttpBinding messages are encrypted by default and achieve message level security. This binding supports reliability, transactions, and security over the internet. It supports HTTP or HTTPS protocols and text as well as MTOM encoding.

    The difference between basicHttpBinding and WsHttpBinding is WsHttpBinding does support WS-* standards like WS-Addressing, WS-Security, and WS-ReliableMessaging

     

  3. wsDualHttpBinding

    wsDualHttpBinding is best when you required bidirectional communication with the client. In some scenarios when client makes a call to WCF service after processing running request service has to call client application for example, updating shipment details to client application.

    This binding supports reliability, transactions, and security over the internet. It supports HTTP or HTTPS protocols and text as well as MTOM encoding. You can implement a Duplex message exchange pattern with wsDualHttpBinding.

  4. webHttpBinding

    webHttpBinding is best when you wish to implement a RESTful WCF service. This is secure and interoperable binding which sends information directly over HTTP or HTTPS without creating SOAP messages. It allows HTTP request to use plain old XML (POX) style messaging which reduces the message size on wire compare to SOAP messages.

  5. NetTcpBinding

    netTcpBinding is best when WCF service and its clients are in intranet infrastructure. As it supports only TCP protocol and not HTTP so service cannot be accessed over the internet.

    This is secure binding is used to send binary encoded SOAP messages within intranet computers. It supports reliability, transaction, and security. If you are using netTcpBinding and host WCF service in IIS, you need to make some settings on the system and IIS this article will help you with required settings.

  6. netNamedPipeBinding

    When your WCF service and its clients reside on the same computer netNamedPipeBinding is the best choice and gives the best performance over other bindings. This is secure bindings. Binary encoded SOAP messages are sent over named pipes.

    See how to implement netNamedPipeBinding in WCF services.

  7. netPeerTcpBinding

    netPeerTcpBinding is best when you require more security for peer to peer communication as netTcpBinding does not provide it. It is secure binding and supports TCP protocols.

  8. WsFederationHttpBinding

    It is secure and interoperable binding supports federated security. It supports HTTP and HTTPS transport protocols as well as text and MTOM encodings.

  9. NetMsmqBinding

    netMsmqBinding is best when you have to execute service operations in a queued manner. Service requests are placed in queue and executed one by one. With netMsmqBinding service operations will always be one way and do not return any response to client.

    This is interoperable bindings and can be used on existing MSMQ applications that use COM or Application Programing Interface(API)

In real time applications, you have to use multiple bindings for one service endpoint. For example internet applications should be able to access service through HTTP requests at the same time the back office application should be able to access service by netTcpBinding or netNamedPipeBinding to get performance benefits.