OperationContractAttribute.AsyncPattern Property
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Indicates that an operation is implemented asynchronously using a Begin<methodName> and End<methodName> method pair in a service contract.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
Property Value
Type: System.Booleantrue 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 operations that can be called asynchronously. 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 Windows Communication Foundation (WCF) 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. 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.
// A service contract that is defined with an interface
// using an aysnchronous pattern for Windows Phone applications
// and a synchronous pattern otherwise.
[ServiceContract(
Name = "SampleServiceContract1",
Namespace = "http://microsoft.wp.documentation")]
public interface IService1
{
#if WINDOWS_PHONE
[OperationContract(AsyncPattern = true)]
IAsyncResult BeginGetPerson(int personId, AsyncCallback callback, object state);
Person EndGetPerson(IAsyncResult result);
#else
[OperationContract]
Person GetPerson(int personId);
#endif
}
[DataContract]
public class Person
{
[DataMember]
public string Name;
[DataMember]
public int Age;
}