DataServiceContext.EndExecute<TElement>(IAsyncResult) メソッド

定義

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)

型パラメーター

TElement

クエリによって返される型。

パラメーター

asyncResult
IAsyncResult

IAsyncResult オブジェクト。

返品

IEnumerable<TElement>

クエリ操作によって返される結果。

例外

asyncResultnullされたとき。

asyncResultがこのDataServiceContext インスタンスから生成されなかった場合。

-または-

EndExecute<TElement>(IAsyncResult) メソッドは以前に呼び出されました。

要求の実行中または応答メッセージの内容をオブジェクトに変換するときにエラーが発生した場合。

次の例では、 BeginExecute メソッドを呼び出して非同期クエリを実行してクエリを開始する方法を示します。 インライン デリゲートは、 EndExecute メソッドを呼び出してクエリ結果を表示します。 この例では、Northwind データ サービスに基づいてサービス参照の追加ツールによって生成された DataServiceContext を使用します。これは、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

注釈

標準の begin-end 非同期パターンに従って、クエリ結果が取得されると、指定されたコールバックが呼び出されます。 詳細については、「 非同期操作」を参照してください。

コールバックが呼び出されると、すべての結果が HTTP ストリームから読み取られますが、処理されていません。ローカルユーザー向けオブジェクトが具体化または変更されておらず、ID 解決は行われません。 EndExecuteが呼び出されると、DataServiceResponseが作成されて返されますが、結果はまだ処理されていません。 ID 解決、オブジェクトの具体化、および操作は、ユーザーが結果を列挙した場合にのみ発生します。

適用対象

こちらもご覧ください