DataServiceContext.EndExecute<TElement>(IAsyncResult) 方法

定义

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)

类型参数

TElement

查询返回的类型。

参数

asyncResult
IAsyncResult

IAsyncResult 对象。

返回

IEnumerable<TElement>

查询操作返回的结果。

例外

何时 asyncResultnull

何时 asyncResult 未源自此 DataServiceContext 实例。

-或-

以前 EndExecute<TElement>(IAsyncResult) 调用过该方法。

在执行请求期间或将响应消息的内容转换为对象时引发错误时。

示例

以下示例演示如何通过调用 BeginExecute 方法来启动查询来执行异步查询。 内联委托调用 EndExecute 方法以显示查询结果。 此示例使用 DataServiceContext 基于 Northwind 数据服务生成的“添加服务引用”工具,该工具是在完成 WCF 数据服务时创建的。

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

注解

根据标准开始端异步模式,在检索查询结果时调用提供的回调。 有关详细信息,请参阅 异步操作

调用回调时,所有结果都已从 HTTP 流中读取,但尚未处理这些结果;未具体化或修改面向本地用户的对象,并且未发生标识解析。 调用时 EndExecute ,将创建并返回 a DataServiceResponse ,但结果仍未得到处理。 仅当用户枚举结果时,才会发生标识解析、对象具体化和操作。

适用于

另请参阅