Dailycode.info

Short solution for short problems

WCF: Which protocol to use? HTTP or TCP?

My overall conclusion of reading information and using both protocols is that you have to consider these to topics before choosing the protocol to use!

  • Are the services used internally or will they be used by external applications?
  • Is it an internet application?

When you answer yes to one of the previous questions, using the HTPP protocol will be the best choice. But when you develop a client server application for use within the company, use TCP.  TCP is in my opinion the best protocol to use when client and server use WCF and the application is running on a local network. The services can also be accessed by other applications running on this network and using WCF. So I believe that in many cases people just tend to HTTP without really looking why. Next I list a part from a Microsoft article, which explains this for you clear and brief:

When to Use HTTP Transport 

HTTP is a request/response protocol between clients and servers. The most common application consists of Web-browser clients that communicate with a Web server. The client sends a request to a server, which listens for client request messages. When the server receives a request, it returns a response, which contains the status of the request. If successful, optional data, such as a Web page, an error message, or other information is returned. For more information about the HTTP protocol, see HTTP - Hypertext Transfer Protocol.

The HTTP protocol is not connection-based—once the response is sent, no state is maintained. To handle multiple-page transactions, the application must persist any necessary state.

In WCF, the HTTP transport binding is optimized for interoperability with legacy non-WCF systems. If all communicating parties are using WCF, the TCP-based or named pipes-based bindings are faster. For more information, see NetTcpBinding and NetNamedPipeBinding.

When to Use the TCP Transport 

TCP is a connection-based, stream-oriented delivery service with end-to-end error detection and correction. Connection-based means that a communication session between hosts is established before exchanging data. A host is any device on a TCP/IP network identified by a logical IP address.

TCP provides reliable data delivery and ease of use. Specifically, TCP notifies the sender of packet delivery, guarantees that packets are delivered in the same order in which they are sent, retransmits lost packets, and ensures that data packets are not duplicated. Note that this reliable delivery applies between two TCP/IP nodes, and is not the same thing as WS-ReliableMessaging, which applies between endpoints, no matter how many intermediate nodes they may include.

The WCF TCP transport is optimized for the scenario where both ends of the communication are using WCF. This binding is the fastest WCF binding for scenarios that involve communicating between different machines. The message exchanges use the BinaryMessageEncodingBindingElement for optimized message transfer. TCP provides duplex communication and so can be used to implement duplex contracts, even if the client is behind network address translation (NAT).

 

Fast way to test your service is to open the visual studio command prompt and type in WCFTESTCLIENT This will open the WCFTestclient.exe and there you can test your endpoints.