Share via


DataServiceContext.EndExecute<TElement> 方法 (IAsyncResult)

调用后可完成 BeginExecute

命名空间:  System.Data.Services.Client
程序集:  Microsoft.Data.Services.Client(在 Microsoft.Data.Services.Client.dll 中)

语法

声明
Public Function EndExecute(Of TElement) ( _
    asyncResult As IAsyncResult _
) As IEnumerable(Of TElement)
用法
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 不支持一般类型和方法。

类型参数

  • TElement
    查询所返回的类型。

参数

返回值

类型:System.Collections.Generic.IEnumerable<TElement>
查询操作所返回的结果。

异常

异常 条件
ArgumentNullException

当 asyncResult 为 nullnull 引用(在 Visual Basic 中为 Nothing) 时。

ArgumentException

当 asyncResult 不是源自此 DataServiceContext 实例时。

- 或 -

当以前调用过 EndExecute 方法时。

InvalidOperationException

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

DataServiceQueryException

当数据服务返回“HTTP 404: 未找到资源”错误时。

注释

根据标准的 begin-end 异步模式,在检索查询结果时将调用所提供的回调。 有关详细信息,请参阅异步操作(WCF 数据服务)

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

示例

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

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

请参阅

参考

DataServiceContext 类

EndExecute 重载

System.Data.Services.Client 命名空间

其他资源

如何:执行异步数据服务查询(WCF 数据服务)