Share via


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 メソッドを呼び出してクエリ結果を表示します。 この例では、DataServiceContextNorthwind データ サービスに基づいてサービス参照の追加ツールによって生成された を使用します。これは、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 解決、オブジェクトの具体化、および操作は、ユーザーが結果を列挙したときにのみ行われます。

適用対象

こちらもご覧ください