Export (0) Print
Expand All

DataServiceContext.EndExecute<TElement> Method

Called to complete the BeginExecute.

Namespace:  System.Data.Services.Client
Assembly:  System.Data.Services.Client (in System.Data.Services.Client.dll)

public IEnumerable<TElement> EndExecute<TElement>(
	IAsyncResult asyncResult
)

Type Parameters

TElement

The type returned by the query.

Parameters

asyncResult
Type: System.IAsyncResult

IAsyncResult object.

Return Value

Type: System.Collections.Generic.IEnumerable<TElement>
The results returned by the query operation.

ExceptionCondition
ArgumentNullException

When asyncResult is null.

ArgumentException

When asyncResult did not originate from this DataServiceContext instance.

-or-

When the EndExecute<TElement> method was previously called.

InvalidOperationException

When an error is raised either during execution of the request or when it converts the contents of the response message into objects.

According to the standard begin-end asynchronous pattern, the provided callback is invoked when query results are retrieved. For more information, see Asynchronous Operations (ADO.NET Data Services).

When the callback is invoked, all results have been read from the HTTP stream, but they have not been processed; no local user-facing objects have been materialized or modified and identity resolution has not occurred. When EndExecute<TElement> is invoked, a DataServiceResponse is created and returned but the results still have not been processed. Identity resolution, object materialization, and manipulation occur only when the user enumerates the results.

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 ADO.NET 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<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);
        }
    }
}    

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1

Community Additions

ADD
Show:
© 2014 Microsoft