After saying the asynchronous service invocation of the client (see WCF Technical profiling: Application of asynchronous operations in WCF (above)), we're talking about how the server-side provides implementations for services in an asynchronous manner. When defining a service contract, I believe you have noticed that the OperationContractAttribute feature has a bool type of asynpattern. This property defines a service operation as an asynchronous implementation pattern, followed by a focus on the definition and implementation of asynchronous operations.
The definition and implementation principle of asynchronous operation
The implementation of the WCF asynchronous service operation pattern has some programmatic limitations: Asynchronous service operations are implemented through two pairing methods and are typically named by asynchronous operations: Beginxxx/endxxx. Two methods need to adopt the following signature, the Asyncpattern attribute is specified OperationContractAttribute only need to apply to the BeginXxx method.
1: [OperationContract (Asyncpattern = True)]
2:iasyncresult begindowork (Parameters, AsyncCallback usercallback, Object stateobject);
3:returntype enddowork (IAsyncResult asynresult);
For example, the following two pieces of code can be seen as the same operation in synchronous and asynchronous different performance.
1: [OperationContract]
2:double ADD (double x, double y);
1: [OperationContract (Asyncpattern = True)]
2:iasyncresult BeginAdd (double x, double y, AsyncCallback usercallback, Object stateobject);
3:double EndAdd (IAsyncResult asynresult);
After understanding the definition pattern of asynchronous operations, let's talk about the principle of the implementation of the WCF asynchronous operation. WCF represents a description of a service operation through a type operationdescription. As shown in the following code, the OperationDescription has 3 important memthodinfo types of property Members: Syncmethod, BeginMethod, and EndMethod, which represent synchronous methods, asynchronous start and End methods, respectively. Take the code above as an example, if you use Syncmethod to represent the Add method, and BeginMethod and EndMethod correspond to the BeginAdd and EndAdd methods.
1:public class OperationDescription
2: {
3:
4: Public MethodInfo Syncmethod {get; set;}
5: Public MethodInfo BeginMethod {get; set;}
6: Public MethodInfo EndMethod {get; set;}
7: //other Members
8:}
WCF selects the appropriate operation through the Operationselector and executes the method corresponding to the selected operation through Operationinvoker. All the Operationinvoker implements the interface System.ServiceModel.Dispatcher.IOperationInvoker. Here is the basic definition of ioperationinvoker. Invoke and Invokebegin/invokeend represent synchronous and asynchronous execution of the operation, IsSynchronous indicates whether the current operation is asynchronous, and if the asyncpattern of the operation is true it indicates an asynchronous operation.
1:public Interface IOperationInvoker
2: {
3: object[] allocateinputs ();
4: Object Invoke (object instance, object[] inputs, out object[] outputs);
5: IAsyncResult Invokebegin (object instance, object[] inputs, AsyncCallback callback, object state);
6: Object Invokeend (object instance, out object[] outputs, IAsyncResult result);
7: bool issynchronous {get;}
8:}
Two typical operationinvoker:syncoperationinvoker and asyncoperationinvoker are defined in WCF for the execution of synchronous and asynchronous operations, respectively. All two operationinvoker implements the IOperationInvoker interface, Syncoperationinvoker implements the Invoke method, Asyncoperationinvoker realized Invokebegin and invokeend.
When the correct method is chosen through Operationselector and Instanceprovider and the corresponding service instance is obtained, WCF chooses the corresponding operationinvoker according to the asyncpattern of the operation. If it is synchronized, then natural selection syncoperationinvoker and executes the Invoke method. The Invoke method obtains the methodinfo of the synchronous operation method through the OperationDescription Syncmethod attribute, and adopts the reflection mechanism to execute the method; for asynchronous operations, The Asyncoperationinvoker Invokebegin and Invokeend methods are invoked. The methodinfo corresponding to the Invokebegin and Invokeend methods are obtained through OperationDescription BeginMethod and EndMethod properties. When the corresponding MethodInfo object is obtained, the service instance is also invoked through reflection.
Ii. How to create an asynchronous service
After understanding the definition of asynchronous operations and the specific implementation principles, we demonstrate the implementation of asynchronous operations in WCF applications through a simple example. In this example, we use the service to read the server file, in the implementation of the file read operation, the asynchronous file read mode.
Let's look at the definition of a service contract first. The service contract is defined by the interface Ifilereader, and file read operations based on file names are defined asynchronously in the BeginRead and EndRead methods.
1:using System;
2:using System.ServiceModel;
3:namespace Artech.AsyncServices.Contracts
4: {
5: [ServiceContract (namespace= "http://www.artech.com/")]
6: Public interface Ifilereader
7: {
8: [OperationContract (Asyncpattern = True)]
9: IAsyncResult beginread (String fileName, AsyncCallback usercallback, Object stateobject);
10:
One: string EndRead (IAsyncResult asynresult);
: }
13:}
FileReader implements the contract contract, in BeginRead method, creates FileStream object according to file name, invokes FileStream BeginRead method to realize asynchronous reading of file, and directly returns the execution result of the method: a IAsyncResult object. In the EndRead method, call FileStream's endread to read the contents of the file and close the FileStream object.
1:using System;
2:using System.Text;
3:using Artech.AsyncServices.Contracts;
4:using System.IO;
5:namespace Artech.AsyncServices.Services
6: {
7:public class Filereaderservice:ifilereader
8: {
9:private Const string baselocation = @ "E:\";
10:private FileStream _stream;
11:private byte[] _buffer;
12:
#region Ifilereader Members
14:
15:public IAsyncResult BeginRead (String fileName, AsyncCallback usercallback, Object StateObject)
16: {
17:this._stream = new FileStream (baselocation + fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
18:this._buffer = new Byte[this._stream. Length];
19:return This._stream. BeginRead (this._buffer, 0, This._buffer. Length, Usercallback, stateobject);
20:}
21st:
22:public string EndRead (IAsyncResult ar)
23: {
24:this._stream. EndRead (AR);
25:this._stream. Close ();
26:return Encoding.ASCII.GetString (This._buffer);
27:}
28:
: #endregion
30:}
31:}