My WCF journey (3): Implement duplex communication in WCF

Source: Internet
Author: User

 

The Duplex mode message exchange mode is reflected in the message exchange process. Both parties can send messages to the other party. Message exchange based on duplex MEP can be seen as a combination of message exchange in multiple basic modes (such as request-reply mode and single mode. Duplex MEP has some variants. For example, a typical subscription-publishing mode can be seen as a manifestation of duplex mode. The Duplex message exchange mode makes it possible for the server to call back the client.

I. Two Typical duplex MEP

1. Callback during the request process

This is a typical form of Duplex message exchange mode. When a client calls a service, it attaches a callback object. The service is processing this, call back the operation of the client through the callback object appended to the client (actually the proxy object that calls the callback service) (this operation is executed on the client ). The entire message exchange process is actually composed of two basic message exchanges. One is the normal service request of the client, and the other is the callback of the server to the client. The two can adopt the request-reply mode, or One-way MEP for message exchange.Figure 1This process is described. Both the service call and callback adopt the request-reply MEP.



Figure 1 callback in the Request Process

2. Subscription-publish

Subscription-publish mode is a typical variant of duplex mode. In this mode, both parties of the message exchange become subscribers and publishers. Several subscribers apply to the publisher for a topic, and the publisher saves all subscribers in a subscriber list, send a topic to all subscribers of the topic at a certain time. In fact, the message exchange based on the subscription-publish mode can also be seen as a combination of message exchange in two basic modes. The application subscription mode is a one-way message exchange mode (if the subscriber behavior gets feedback from the subscription, the message exchange mode can also be used). Topic publishing is also a one-way message exchange process. The subscription-publish message exchange mode is shown in figureFigure 2.


Figure 2 Subscription-publish

 

Ii. Example demonstration: Create a WCF Application Based on duplex communication

Next, we will use a case to demonstrate a duplex communication-based WCF application. For simplicity, we use the computing service example. Before that, we call CalculuateService to directly obtain the calculation result and output the calculation result through the console. In this example, we will use another completely different method to call the service and output the result: One-way) in this mode, CalculuateService (that is, the client cannot obtain the computing result by replying to a message) is called. After the calculation result is completed, the server prints the computing result on the client by calling back. The entire application layer still adopts our consistent 4-layer structure: Contracts, Services, Hosting, and Clients, suchFigure 3.


Figure 3 duplex communication case application structure

 

Step 1: Define the service contract and callback contract

First, define the service contract. As shown in the following example, the service contract is defined using an interface (ICalculator) to act on the Add operation of the specified addition operation, we define an operation as one-way through the IsOneway attribute of aspx "target = _ blank> OperationContractAttribute. This means that the client only sends an operation request to the server, no calculation result is obtained by replying to the message.

   1: using System.ServiceModel;
   2: namespace Artech.DuplexServices.Contracts
   3: {
   4:     [ServiceContract(Namespace="http://www.artech.com/",
   5: CallbackContract=typeof(ICallback))]
   6:     public interface ICalculator
Related Article

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.