How to get started with WCF 3--wcf Communication Mode

Source: Internet
Author: User

How to get started with WCF 3--wcf Communication Mode

Content of this Chapter

  • Request/response mode
  • Ticket Mode
  • Duplex Mode
  • WCF asynchronous call
Request and response modes

Request/Response
Request/response communication means that after a client sends a message to the server, the server sends a response to the client. This also means that the client will not continue to perform operations until the service response is received.

[OperationContract]string getOrders(DateTime dt);

The request/response mode is the default message mode.
The return value is void, which is also a request/response message exchange.
Unless the client calls the operation asynchronously, the client stops processing until it receives the returned message, even if the message is normally empty.
Disadvantages
-If the operation takes a long time, the client performance and response capabilities will be reduced.
Advantages
-A soap error may be returned in the Response Message, indicating that some service-related errors may occur during communication or processing.

Ticket Mode

One-way communication refers to communication only in one direction, that is, from the client to the service. The service does not send a response, and the client does not expect a response. In this case, the client sends the message and continues to execute it.
One-way operations are operations that the client calls and continues to process after WCF writes messages to the network. This usually means that, unless the data sent in the outbound message is extremely large, the client will continue running almost immediately (unless an error occurs when sending the data ). This type of message exchange mode supports event-like behavior from the client to the service application.
To specify one-way message exchange for operations that return void, set the IsOneWay attribute to true. The default value is false.

[OperationContract(IsOneWay=true)]void insertEmp(Employee emp);
Duplex Mode

The Duplex Mode features that the service and client can independently send messages to each other, regardless of whether one-way message transmission or request/reply message transmission is used. This form of bidirectional communication is useful for services that must communicate directly with the client or provide an asynchronous experience (including event-like behavior) to any party in the message exchange.

-To design a duplex protocol, you must also design a callback protocol and assign the type of the callback protocol to the CallbackContract attribute (property) of the ServiceContractAttribute attribute that marks the service agreement ).

-To implement the duplex mode, you must create the second interface, which contains the method declaration called by the client.

[ServiceContract(SessionMode=SessionMode.Required,CallbackContract=typeof(IServiceDuplexCallback))]public interface IServiceClass{[OperationContract(IsOneWay = true)]void AddNumber();}public interface IServiceDuplexCallback{[OperationContract(IsOneWay = true)]void Calculate(double result);}
WCF asynchronous call mode

Two asynchronous methods of WCF
Use svcutil to generate an asynchronous proxy class
Modify interface definition Asynchronous Method
Asynchronization and thread
Advantages and disadvantages of asynchronous operations
Asynchronous call)
The operating system has been very sophisticated today, and threads are one of the masterpiece. The operating system divides the CPU processing time into many short time slices. At time T1, it executes the instruction of a thread, and at time T2, it executes the instruction of the next thread, and each thread executes the instruction in turn, the result is that all threads are working side by side. In this way, multiple threads can be created during programming and executed during the same period. Each thread can perform different tasks in parallel.
In the single-threaded mode, a computer is a strictly von noriman machine. When one piece of code calls another piece of code, it can only be called synchronously. After the code is executed, the call can continue to be executed.
With multi-thread support, asynchronous calls can be used. The caller and the called can belong to two different threads. After the caller starts the called thread, wait until the other party returns the result to continue executing the subsequent code.
For example, the Read method of the FileStream object
Synchronization mode
Int Read (byte [] buffer, int offset, int count)
Asynchronous mode
IAsyState BeginRead (byte [] buffer, int offset, int count, IAsyncResult callback, Object asyState)
Int EndRead (IAsyncResult ar)

Two asynchronous methods of WCF
Asynchronous or not is determined by the client. At the beginning of the design, the method called by the Service caller should not be considered.
WCF can generate proxy classes for asynchronous client calls in two ways:
1. Use svcutil to generate an asynchronous proxy class
2. Modify the interface definition Asynchronous Method
When svcutil is used to generate client proxy code, you only need to add/async to generate proxy classes with asynchronous functions.
In IDE, the operation is simpler, that is, when adding ServiceReference, select the advanced option and select "create Asynchronous Method"
Example:

// Asynchronous private void button#click (object sender, EventArgs e) {myService. service1Client client = new client. myService. service1Client (); client. helloCompleted + = new EventHandler <client. myService. helloCompletedEventArgs> (client_helloCompleted); client. helloAsync ("my god"); // client. beginhello ("my god", doCallBack, client);} void client_helloCompleted (object sender, client. myService. helloCompletedEventArgs e) {MessageBox. show (e. result);} // private void doCallBack (IAsyncResult ar) // {// string s = (myService. service1Client) ar. asyncState ). endhello (ar); // MessageBox. show (s );//}

Asynchronous call method 2
Modify the service definition on the client and add asynchronous methods for operations.
When the client decides to use an Asynchronous Method to call a service operation, although the service contract interface of the client needs to be modified, the service contract definition of the server is not affected.

[OperationContract(AsyncPattern = true, Action = "",ReplyAction="")]IAsyncResult BeginTransferFile(AsyncCallback callback, object asyncState);byte[] EndTransferFile(IAsyncResult result);

 

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.