Επεξεργασία

Κοινή χρήση μέσω


OperationContractAttribute.AsyncPattern Property

Definition

Indicates that an operation is implemented asynchronously using a Begin<methodName> and End<methodName> method pair in a service contract.

public:
 property bool AsyncPattern { bool get(); void set(bool value); };
public bool AsyncPattern { get; set; }
member this.AsyncPattern : bool with get, set
Public Property AsyncPattern As Boolean

Property Value

true if the Begin<methodName>method is matched by an End<methodName> method and can be treated by the infrastructure as an operation that is implemented as an asynchronous method pair on the service interface; otherwise, false. The default is false.

Examples

The following code example shows a client channel to a service contract that includes both a synchronous version of Add and an asynchronous version. If the contract interface is used on the client, both the BeginAdd and the Add operation invoke a method on the server that may or may not be synchronous. If the contract is used to implement the service, the default is that incoming requests are dispatched to the synchronous method.

[ServiceContract]  
public interface IAddTwoNumbers  
{  
    // If the asynchronous method pair  
    // appears on the client channel, the client can call   
    // them asynchronously to prevent blocking.  
    [OperationContract (AsyncPattern=true)]  
    IAsyncResult BeginAdd(int a, int b, AsyncCallback cb, AsyncState s);  

    [OperationContract]  
    int EndAdd(IAsyncResult r);  

    // This is a synchronous version of the BeginAdd/EndAdd pair.  
    // It appears in the client channel code by default.   
    [OperationContract]  
    int Add(int a, int b);  
   }  

Remarks

Use the AsyncPattern property to build service operations that can be called asynchronously on the server, the client, or both. The AsyncPattern property informs the runtime that a Begin method has a matched End method that conforms to the .NET Framework asynchronous method design pattern. Building server asynchronous methods that implement a service operation increases server scalability and performance without affecting the clients of the service, and is recommended when a service operation must return something to the client after performing a lengthy operation that can be performed asynchronously.

Clients remain unaffected because the asynchronous method pair on the server is an implementation detail that does not affect the underlying Web Services Description Language (WSDL) description of the operation. Such methods appear to clients as a single operation with <input> and correlated <output> messages. WCF automatically routes inbound messages to the Begin<methodName> method and routes the results of the End<methodName> call to the outbound message. Client channels, therefore, can represent the method pair as either a single synchronous operation or as an asynchronous operation pair. In no case does the client representation affect the asynchronous implementation on the server in any way.

Client contracts can use the AsyncPattern property to indicate an asynchronous method pair that the client can use to invoke the operation asynchronously. Typically, client applications use the ServiceModel Metadata Utility Tool (Svcutil.exe) tool and the /async option to generate a Begin<methodName> and End<methodName> method pair that the client can use to invoke the operation asynchronously.

Note

If a service operation has both an asynchronous and a synchronous version, the default behavior on the service is to invoke the synchronous version.

Applies to