Share via


Asynchronous Operations (ADO.NET 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 classes when accessing ADO.NET Data Services servers over the Web.

Although the ADO.NET Data Services servers process HTTP requests asynchronously, some methods of the ADO.NET Data Services client libraries are synchronous and wait until the entire request-response exchange is completed before continuing execution. The asynchronous methods of the ADO.NET Data Services client libraries do not wait for this exchange to complete and can thus 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 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 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 instance as the state parameter when you call the BeginExecute(AsyncCallback, Object) method on the instance, the same DataServiceQuery instance is returned by the IAsyncResult. This instance of DataServiceQuery is then used to call the EndExecute(IAsyncResult) method to complete the query operation. For more information, see How to: Execute Asynchronous Data Service Queries (ADO.NET Data Services).

Note

Only asynchronous operations are supported by the client libraries that are provided in the .NET Framework for Silverlight. For more information, see ADO.NET Data Services (Silverlight).

The .NET Framework client libraries support the following asynchronous operations:

Operation

Methods

Executing a DataServiceQuery.

  • BeginExecute(AsyncCallback, Object)

  • EndExecute(IAsyncResult)

Executing a query from the DataServiceContext.

  • BeginExecute``1(Uri, AsyncCallback, Object)

  • EndExecute``1(IAsyncResult)

Executing a batch query from the DataServiceContext.

  • BeginExecuteBatch(AsyncCallback, Object, array<DataServiceRequest[])

  • EndExecuteBatch(IAsyncResult)

Loading a related entity into the DataServiceContext.

  • BeginLoadProperty(Object, String, AsyncCallback, Object)

  • EndLoadProperty(IAsyncResult)

Saving changes to objects in the DataServiceContext

  • BeginSaveChanges(SaveChangesOptions, AsyncCallback, Object)

  • EndSaveChanges(IAsyncResult)

Threading Considerations for Asynchronous Operations

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(Delegate, array<Object[]) method on the Dispatcher object. For more information, see Querying the Data Service (ADO.NET Data Services/Silverlight).

See Also

Other Resources

Using a Data Service in a .NET Framework Application (ADO.NET Data Services)