次の方法で共有


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 Data Services)」を参照してください。

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

使用例

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

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 Data Services)