DataServiceContext.BeginExecute<TElement> Method (Uri, AsyncCallback, Object)
Asynchronously sends the request so that this call does not block processing while waiting for the results from the service.
Namespace: System.Data.Services.Client
Assembly: System.Data.Services.Client (in System.Data.Services.Client.dll)
public IAsyncResult BeginExecute<TElement>(
Uri requestUri,
AsyncCallback callback,
Object state
)
Type Parameters
- TElement
The type returned by the query.
Parameters
- requestUri
- Type: System.Uri
The URI to which the query request will be sent. The URI may be any valid data service URI; it can contain $ query parameters.
- callback
- Type: System.AsyncCallback
Delegate to invoke when results are available for client consumption.
- state
- Type: System.Object
User-defined state object passed to the callback.
Return Value
Type: System.IAsyncResultAn object that is used to track the status of the asynchronous operation.
The returned IAsyncResult object is used to determine when the asynchronous operation has completed. For more information, see Asynchronous Operations (WCF Data Services).
The method BeginExecute uses the same semantics as Execute, however this method asynchronously sends the request so that this call does not block processing while waiting for the results from the service. According to the standard begin-end asynchronous pattern, the provided callback is invoked when query results are retrieved.
The following example shows how to execute an asynchronous query by calling the BeginExecute method to start the query. The inline delegate calls the EndExecute method to display the query results. This example uses the DataServiceContext generated by the Add Service Reference tool based on the Northwind data service, which is created when you complete the WCF Data Services quickstart.
public static void BeginExecuteCustomersQuery() { // Create the DataServiceContext using the service URI. NorthwindEntities context = new NorthwindEntities(svcUri); // Define the query to execute asynchronously that returns // all customers with their respective orders. DataServiceQuery<Customer> query = (DataServiceQuery<Customer>)(from cust in context.Customers.Expand("Orders") where cust.CustomerID == "ALFKI" select cust); try { // Begin query execution, supplying a method to handle the response // and the original query object to maintain state in the callback. query.BeginExecute(OnCustomersQueryComplete, query); } catch (DataServiceQueryException ex) { throw new ApplicationException( "An error occurred during query execution.", ex); } } // Handle the query callback. private static void OnCustomersQueryComplete(IAsyncResult result) { // Get the original query from the result. DataServiceQuery<Customer> query = result as DataServiceQuery<Customer>; foreach (Customer customer in query.EndExecute(result)) { Console.WriteLine("Customer Name: {0}", customer.CompanyName); foreach (Order order in customer.Orders) { Console.WriteLine("Order #: {0} - Freight $: {1}", order.OrderID, order.Freight); } } }
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.