Condividi tramite


How to: Execute Asynchronous Data Service Queries (ADO.NET Data Services)

By using the ADO.NET Data Services client library, you can asynchronously perform client-server operations, such as executing queries and saving changes. For more information, see Asynchronous Operations (ADO.NET Data Services).

Note

In an application where the callback must be invoked on a specific thread, you must explicitly marshal the execution of the EndExecute(IAsyncResult) method. For more information, see Asynchronous Operations (ADO.NET Data Services).

The example in this topic uses the Northwind sample data service and autogenerated client data service classes. This service and the client data classes are created when you complete the ADO.NET Data Services quickstart.

Example

The following example shows how to execute an asynchronous query by calling the BeginExecute(AsyncCallback, Object) method to start the query. The inline delegate calls the EndExecute(IAsyncResult) method to display the query results.

Public Shared Sub BeginExecuteCustomersQuery()
    ' Create the DataServiceContext using the service URI.
    Dim context = New NorthwindEntities(svcUri)

    ' Define the delegate to callback into the process
    Dim callback As AsyncCallback = AddressOf OnCustomersQueryComplete

    ' Define the query to execute asynchronously that returns 
    ' all customers with their respective orders.
    Dim query As DataServiceQuery(Of Customers) = _
    context.Customers.Expand("Orders")

    Try
        ' Begin query execution, supplying a method to handle the response
        ' and the original query object to maintain state in the callback.
        query.BeginExecute(callback, query)
    Catch ex As DataServiceQueryException
        Throw New ApplicationException( _
                "An error occurred during query execution.", ex)
    End Try
End Sub
' Handle the query callback.
Private Shared Sub OnCustomersQueryComplete(ByVal result As IAsyncResult)
    ' Get the original query from the result.
    Dim query As DataServiceQuery(Of Customers) = _
        CType(result, DataServiceQuery(Of Customers))

    ' Complete the query execution.
    For Each customer As Customers In query.EndExecute(result)
        Console.WriteLine("Customer Name: {0}", customer.CompanyName)
        For Each order As Orders In customer.Orders
            Console.WriteLine("Order #: {0} - Freight $: {1}", _
                    order.OrderID, order.Freight)
        Next
    Next
End Sub
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<Customers> query = 
        context.Customers.Expand("Orders");

    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 (Exception 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<Customers> query = 
        result as DataServiceQuery<Customers>;

    // Complete the query execution.
    foreach (Customers customer in query.EndExecute(result))
    {
        Console.WriteLine("Customer Name: {0}", customer.CompanyName);
        foreach (Orders order in customer.Orders)
        {
            Console.WriteLine("Order #: {0} - Freight $: {1}",
                order.OrderID, order.Freight);
        }
    }
}    

See Also

Other Resources

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