Asynchronous Operations (WCF Data Services)
Web applications must accommodate higher latency between client and server than applications that run inside internal networks. To optimize the performance and user experience of your application, we recommend using the asynchronous methods of the DataServiceContext and DataServiceQuery<TElement> classes when accessing WCF Data Services servers over the Web.
Although the WCF Data Services servers process HTTP requests asynchronously, some methods of the WCF Data Services client libraries are synchronous and wait until the entire request-response exchange is completed before continuing execution. The asynchronous methods of the WCF Data Services client libraries do not wait for this exchange to complete and can allow your application to maintain a responsive user interface in the meantime.
You can perform asynchronous operations by using a pair of methods on the DataServiceContext and DataServiceQuery<TElement> classes that start with Begin and End respectively. The Begin methods register a delegate that the service calls when the operation is complete. The End methods should be called in the delegate that is registered to handle the callback from the completed operations. When you call the End method to complete an asynchronous operation, you must do so from the same DataServiceQuery<TElement> or DataServiceContext instance that you used to begin the operation. Each Begin method takes a
state parameter that can pass a state object to the callback. This state object is retrieved from the IAsyncResult that is supplied with the callback and is used to call the corresponding End method to complete the asynchronous operation. For example, when you supply the DataServiceQuery<TElement> instance as the
state parameter when you call the BeginExecute method on the instance, the same DataServiceQuery<TElement> instance is returned by the IAsyncResult. This instance of DataServiceQuery<TElement> is then used to call the EndExecute method to complete the query operation. For more information, see How to: Execute Asynchronous Data Service Queries.
Only asynchronous operations are supported by the client libraries that are provided in the .NET Framework for Silverlight. For more information, see WCF Data Services (Silverlight).
The .NET Framework client libraries support the following asynchronous operations:
|Executing a DataServiceQuery<TElement>.||- BeginExecute|
|Executing a query from the DataServiceContext.||- BeginExecute<TElement>|
|Executing a batch query from the DataServiceContext.||- BeginExecuteBatch|
|Loading a related entity into the DataServiceContext.||- BeginLoadProperty|
|Saving changes to objects in the DataServiceContext||- BeginSaveChanges|
In a multi-threaded application, the delegate that is registered as a callback for the asynchronous operation is not necessarily invoked on the same thread that was used to call the Begin method, which creates the initial request. In an application where the callback must be invoked on a specific thread, you must explicitly marshal the execution of the End method, which handles the response, to the desired thread. For example, in Windows Presentation Foundation (WPF)-based applications and Silverlight-based applications, the response must be marshaled back to the UI thread by using the BeginInvoke method on the Dispatcher object. For more information, see Querying the Data Service (WCF Data Services/Silverlight).