How does WCF overcome the limitations of HTTP transmission protocol and implement different message transmission modes?

Source: Internet
Author: User
Tags call back

WCF uses messages as the only means of communication. It supports different Message Exchange modes (MEP: Message Exchange Pattern). The following are typical MEP types: one-Way, Request/Reply, and Duplex. The message is sent to the transport layer by the channel layer of WCF and sent to the destination through the corresponding transmission protocol. For TCP, it can provide a duplex channel, so it can provide native support for the above three MEP protocols. As we all know that the HTTP protocol is based on the Request/Reply mode, how can it break through its own limitations and provide support for One-Way and Duplex message exchange modes?

I. How does HTTP implement One-Way message exchange mode?

The One-Way mode is the simplest message exchange mode, also known as Send/Forget or datax ). The One-Way mode is based on One-Way message transmission from One source to One or more destinations. As shown in the right figure, in One-Way mode, the sender of the message sends the message to the receiver and does not want to receive a reply from the object. The datagram mode has some variants, including the following typical message exchange methods:Single destination Mode(The sender of a message sends the message to a single receiver ),Multi-vote Mode(A message sender sends a message to a series of predefined receivers) andBroadcast mode(Similar to the multi-cast mode, but the receiver has a wider range ). The One-Way mode generally uses an asynchronous message sending method, and does not want to receive the reply message from the other party. In some cases, it does not even care whether the message can be received normally.

However, there must be a clear understanding of HTTP, that is, HTTP can only work in the Request/Reply mode, which is determined by the implementation of its protocol. So how does the Transport layer (HTTP Transport) work when we use HTTP-based binding (BasicHttpBinding, WSHttpBinding, WS2007HttpBinding, and so on) to call One-Way service operations? In fact, it is very simple: the client sends a SOAP-based HTTP Request to the server as usual. After receiving the clear Request, the server returns an empty HTTP Response in the status of 202 (indicating that the successful Request is successfully accepted. The entire request response process is shown in the figure on the left.

In fact, we can use some message interception tools to intercept messages between the client and the server to analyze the actually used message exchange methods between them, here we use Fiddler, a popular HTTP Debug Proxy. Now, we need to use the Fiddler probe to call One-way service operations using the message exchange method. Assume that the service contract is defined as follows:

using System.ServiceModel;

namespace Artech.MessageInspection.Sender

{

    [ServiceContract(Namespace="http://www.artech.com/")]

    interface ICalculator

    {

        [OperationContract(IsOneWay = true)]

        void Add(double x, double y);

    }

}

The code for service implementation, service boarding, and service calling is saved. Now, the client simply calls Add (1, 2) such a simple service operation through the created Service proxy. It reflects the result of Fiddler's interception of this process. It can be seen that the whole process is still a classic Request/Reply method, and an empty HttpResponse is returned. The content of the entire HttpResponse is as follows:

HTTP/1.1 202 Accepted

Content-Length: 0

Server: Microsoft-HTTPAPI/2.0

Date: Tue, 20 Apr 2010 14:31:36 GMT

Ii. How does HTTP implement Duplex message exchange mode?

If the Duplex message exchange mode is used, either party can send messages to the other party during message exchange, as shown in the right figure. Duplex communication makes it possible for the server to call back the client. We are familiar with Duplex communication.Subscription/publish Mode. In subscription/publish mode, the roles of both parties change from traditional senders and receivers to subscribers and consumers. The subscriber sends a subscription message to the consumer to subscribe to a topic. After receiving the subscription message, the consumer adds the subscriber to the subscription list. When a topic is published, the publisher extracts all subscribers of the current topic to broadcast messages to them.

Message exchange relies on network transmission. Different network transmission protocols have different support methods for duplex communication. For TCP, the Protocol itself is a full-duplex network communication protocol, so it can provide native support for duplex communication. However, HTTP itself is a simple request/reply-based network protocol and does not support duplex communication.WCF implements HTTP-based duplex communication through WsDualHttpBinding, which is actually implemented through two HTTP channels.. The Duplex message exchange mode is actually composed of two simple modes (One-Way or Request/Reply. WCF implements the client callback through duplex communication. Assume that the client calls the service in One-way mode, while the server calls back the client in One-Way mode. In this process, normal service calls and Callbacks are implemented in different HTTP channels. From the perspective of message exchange, the client calls the server and the server to call back the client, which is essentially the same. Therefore, from the HTTP transport layer, the real message exchange mode is shown in the figure on the left. If you use tools like Fiddler, you will see that the normal call to the service is an HttpRequest/HttpREsponse (Status: 202), and the callback corresponds to an HttpRequest/HttpREsponse (Status: 202), but the two are in the opposite direction.

Author: Artech
Source: http://artech.cnblogs.com
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.