Share via


DataServiceContext.EndExecute<TElement> Method (IAsyncResult)

Called to complete the BeginExecute.

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

Syntax

'Declaration
Public Function EndExecute(Of TElement) ( _
    asyncResult As IAsyncResult _
) As IEnumerable(Of TElement)
'Usage
Dim instance As DataServiceContext 
Dim asyncResult As IAsyncResult 
Dim returnValue As IEnumerable(Of TElement)

returnValue = instance.EndExecute(asyncResult)
public IEnumerable<TElement> EndExecute<TElement>(
    IAsyncResult asyncResult
)
public:
generic<typename TElement>
IEnumerable<TElement>^ EndExecute(
    IAsyncResult^ asyncResult
)
member EndExecute : 
        asyncResult:IAsyncResult -> IEnumerable<'TElement> 
JScript does not support generic types and methods.

Type Parameters

  • TElement
    The type returned by the query.

Parameters

Return Value

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

Exceptions

Exception Condition
ArgumentNullException

When asyncResult is nulla null reference (Nothing in Visual Basic).

ArgumentException

When asyncResult did not originate from this DataServiceContext instance.

-or-

When the EndExecute 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.

DataServiceQueryException

When the data service returns an HTTP 404: Resource Not Found error.

Remarks

According to the standard begin-end asynchronous pattern, the provided callback is invoked when query results are retrieved. For more information, see Asynchronous Operations (WCF 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 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.

Examples

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 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 Customer) = _
    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 Customer) = _
        CType(result.AsyncState, DataServiceQuery(Of Customer))

    ' Complete the query execution.
    For Each customer As Customer In query.EndExecute(result)
        Console.WriteLine("Customer Name: {0}", customer.CompanyName)
        For Each order As Order 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<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);
        }
    }
}    

See Also

Reference

DataServiceContext Class

EndExecute Overload

System.Data.Services.Client Namespace

Other Resources

How to: Execute Asynchronous Data Service Queries (WCF Data Services)