DataServiceContext.EndExecute<TElement>(IAsyncResult) Metodo

Definizione

Chiamato per completare il metodo BeginExecute<TElement>(Uri, AsyncCallback, Object).

public:
generic <typename TElement>
 System::Collections::Generic::IEnumerable<TElement> ^ EndExecute(IAsyncResult ^ asyncResult);
public System.Collections.Generic.IEnumerable<TElement> EndExecute<TElement> (IAsyncResult asyncResult);
member this.EndExecute : IAsyncResult -> seq<'Element>
Public Function EndExecute(Of TElement) (asyncResult As IAsyncResult) As IEnumerable(Of TElement)

Parametri di tipo

TElement

Tipo restituito dalla query.

Parametri

asyncResult
IAsyncResult

Oggetto IAsyncResult.

Restituisce

IEnumerable<TElement>

Risultati restituiti dall'operazione di query.

Eccezioni

Quando asyncResult è null.

Se asyncResult non è stato originato da questa istanza di DataServiceContext.

-oppure-

Il metodo EndExecute<TElement>(IAsyncResult) è stato chiamato precedentemente.

Se viene generato un errore durante l'esecuzione della richiesta o la conversione del contenuto del messaggio di risposta in oggetti.

Esempio

Nell'esempio seguente viene illustrato come eseguire una query asincrona chiamando il metodo BeginExecute per l'avvio della query. Il delegato inline chiama il metodo EndExecute per visualizzare i risultati della query. In questo esempio viene usato lo DataServiceContext strumento Aggiungi riferimento al servizio basato sul servizio dati Northwind, creato al termine dell'WCF Data Services .

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);
        }
    }
}
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

Commenti

In base al modello asincrono Begin-End standard, il callback fornito viene richiamato in fase di recupero dei risultati della query. Per altre informazioni, vedere Operazioni asincrone.

Se viene richiamato il callback, tutti i risultati sono stati letti dal flusso HTTP, ma non sono stati elaborati. Nessun oggetto locale rivolto all'utente è stato materializzato o modificato e non si è verificata la risoluzione dell'identità. Se viene richiamato EndExecute, un oggetto DataServiceResponse viene creato e restituito, ma i risultati non sono ancora stati elaborati. La risoluzione di identità, la materializzazione di oggetti e la modifica si verificano solo se l'utente enumera i risultati.

Si applica a

Vedi anche