Export (0) Print
Expand All

OperationContractAttribute.AsyncPattern Property

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

Namespace:  System.ServiceModel
Assemblies:   System.ServiceModel.Primitives (in System.ServiceModel.Primitives.dll)
  System.ServiceModel (in System.ServiceModel.dll)

public bool AsyncPattern { get; set; }

Property Value

Type: System.Boolean
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.

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.

NoteNote

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

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);
   }

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft