WCF learning tour-request and Reply Modes and one-way mode (19th)

Source: Internet
Author: User

WCF learning tour-request and Reply Modes and one-way mode (19th)

I. Overview

There are three communication modes in the communication process: request and reply, one-way, and duplex communication. Here we will introduce them one by one.

Ii. Request and Reply Modes

The client sends a request and waits for the response from the server (except for asynchronous calls). The request is in the suspended state until the server receives a response, this method is more flexible than one-way mode, but with high security. It is highly secure because it is a single thread and is suitable for requests with data returned. As shown in (from the network, the rough red lines in the figure do not represent the call order at this time ):

 

The request and Reply Modes are the default WCF mode, as shown in the following code:

/// <Summary> /// request and Reply Modes, default Mode /// </summary> /// <param name = "Id"> book ID </param> /// <returns> </returns> [OperationContract] string getBook (string Id );

 

As long as the feature "[OperationContract]" is used, even if the return value is void, it belongs to the request and reply mode.

Disadvantage: If you use WCF to upload A 2G file in program A, it may be A few hours before you want to execute program B. If the operation takes a long time, the client program's response capability will be greatly reduced.

Advantage: with the returned value, we can return an error message to the client, for example, only receive information such as the ". rar" file.

  Instance:

 

// Server interface using System; using System. collections. generic; using System. linq; using System. runtime. serialization; using System. serviceModel; using System. text; namespace WcfServiceLib {// Note: You can use the "RENAME" command on the "refactor" menu to change the Interface Name "IBookService" in the Code and configuration file at the same time ". [ServiceContract] public interface IBookService {// <summary> // request and reply mode, default Mode /// </summary> /// <param name = "Id"> book ID </param> /// <returns> </returns> [OperationContract] string getBook (string Id );}} // implement public class BookService on the server: IBookService {public string GetBook (string Id) {System. threading. thread. sleep (20000); int bookId = Convert. toInt32 (Id); Books book = SetBook (bookId); string xml = XMLHelper. toXML <Books> (book); return xml;} public Books SetBook (int Id) {Books book = new Books (); book. bookID = Id; book. authorID = 1; book. category = "IBM"; book. price = 39.99 M; book. numberofcopies = 25; book. name = "DB2 database performance adjustment and optimization"; book. publishDate = new DateTime (2015, 2, 23); return book ;}// the client calls using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace WinClient {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void btnGetBook_Click (object sender, EventArgs e) {textBox1.Text + = string. format ("start calling the wcf Service: {0} \ r \ n", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss"); BookServiceReference. bookServiceClient client = new BookServiceReference. bookServiceClient (); string book = client. getBook ("5"); textBox1.Text + = book; textBox1.Text + = string. format ("\ r \ n call end: {0}", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss "));}}}

 

In the preceding example, if we sleep the thread for 20 seconds on the server side and then return to the client, the interval between the two displays the current time must be more than 20 seconds, as shown in 1 and 2:

 

Figure 1

 

Figure 2

 

Ii. unidirectional mode

As the name suggests, the unidirectional mode is a one-way request. After a client sends a message request to the server, the client loses contact with the server. The request end does not care whether the returned results will continue to be executed. That is to say, after the client sends the request, it will continue to execute and will not wait for the server to return the message, and the server receives the message and executes the service. This one-way mode is actually a multi-thread operation, after the client sends a message, the client and the server will execute the message at the same time, so that the messages do not conflict with each other. It is also thread-safe and improves the execution efficiency.
In unidirectional mode, you only need to add the IsOneWay attribute to the method declaration. It indicates that the message is called in unidirectional mode. As shown in:

 

In unidirectional mode, the set value must be displayed in the OpertaionContract attribute. The Code is as follows:

     [OperationContract(IsOneWay = true)]         void ShowName(string name);

 

The advantages and disadvantages are similar to the "request response mode.

Features: Operations marked with IsOneWay = true do not declare output parameters, reference parameters, or return values.

  Instance:

 

// Server interface using System; using System. collections. generic; using System. linq; using System. runtime. serialization; using System. serviceModel; using System. text; namespace WcfServiceLib {// Note: You can use the "RENAME" command on the "refactor" menu to change the Interface Name "IBookService" in the Code and configuration file at the same time ". [ServiceContract] public interface IBookService {// <summary> // request and reply mode, default Mode /// </summary> /// <param name = "Id"> book ID </param> /// <returns> </returns> [OperationContract] string getBook (string Id ); /// <summary> /// ticket mode, display name // </summary> /// <param name = "name"> book name </param> [OperationContract (IsOneWay = true)] void ShowName (string name) ;}// implement using System on the server; using System. collections. generi C; using System. linq; using System. runtime. serialization; using System. serviceModel; using System. text; namespace WcfServiceLib {// Note: use the "RENAME" command on the "refactor" menu to change the class name "BookService" in the code, svc, and configuration file at the same time ". // Note: To start the WCF test client to test the service, select BookService. svc or BookService. svc. cs in Solution Explorer and start debugging. Public class BookService: IBookService {// <summary> // request and reply mode, default Mode /// </summary> /// <param name = "Id"> book ID </param> /// <returns> </returns> public string GetBook (string Id) {System. threading. thread. sleep (20000); int bookId = Convert. toInt32 (Id); Books book = SetBook (bookId); string xml = XMLHelper. toXML <Books> (book); return xml;} public Books SetBook (int Id) {Books book = new Books (); book. bookID = Id; book. authorID = 1; book. category = "IBM"; book. price = 39.99 M; book. numberofcopies = 25; book. name = "DB2 database performance adjustment and optimization"; book. publishDate = new DateTime (2015, 2, 23); return book ;}/// <summary> /// ticket mode, display name /// </summary> /// <param name = "name"> name </param> public void ShowName (string name) {Console. writeLine (string. format ("book name: {0}, date and time {1}", name, DateTime. now. toString ("yyyy-MM-dd HH: mm: ss") ;}}// the client calls using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace WinClient {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void btnGetBook_Click (object sender, EventArgs e) {textBox1.Text + = string. format ("start calling the wcf Service: {0} \ r \ n", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss"); BookServiceReference. bookServiceClient client = new BookServiceReference. bookServiceClient (); string book = client. getBook ("5"); textBox1.Text + = book; textBox1.Text + = string. format ("\ r \ n call end: {0}", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss "));} /// <summary> /// ticket mode /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void buttonOneWay_Click (object sender, eventArgs e) {textBox1.Text + = string. format ("start calling the wcf Service: {0} \ r \ n", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss"); BookServiceReference. bookServiceClient client = new BookServiceReference. bookServiceClient (); client. showName ("science can read books in this way"); textBox1.Text + = string. format ("\ r \ n call end: {0}", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss "));}}}

 

In one-way mode, the most important thing to do with the request response mode is to add the IsOneWay attribute. The running effect is as follows:

 

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.