Share via


DataServiceContext.BeginExecute メソッド

定義

要求をデータ サービスに非同期的に送信して特定の URI を実行します。

オーバーロード

BeginExecute<T>(DataServiceQueryContinuation<T>, AsyncCallback, Object)

要求をデータ サービスに非同期的に送信して、ページングされたクエリ結果のデータの次のページを取得します。

BeginExecute<TElement>(Uri, AsyncCallback, Object)

サービスからの結果の待機中にこの呼び出しによって処理がブロックされないように、要求を非同期的に送信します。

BeginExecute<T>(DataServiceQueryContinuation<T>, AsyncCallback, Object)

要求をデータ サービスに非同期的に送信して、ページングされたクエリ結果のデータの次のページを取得します。

public:
generic <typename T>
 IAsyncResult ^ BeginExecute(System::Data::Services::Client::DataServiceQueryContinuation<T> ^ continuation, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginExecute<T> (System.Data.Services.Client.DataServiceQueryContinuation<T> continuation, AsyncCallback callback, object state);
member this.BeginExecute : System.Data.Services.Client.DataServiceQueryContinuation<'T> * AsyncCallback * obj -> IAsyncResult
Public Function BeginExecute(Of T) (continuation As DataServiceQueryContinuation(Of T), callback As AsyncCallback, state As Object) As IAsyncResult

型パラメーター

T

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

パラメーター

continuation
DataServiceQueryContinuation<T>

データ サービスから返すデータの次のページを表す DataServiceQueryContinuation<T> オブジェクト。

callback
AsyncCallback

クライアントが結果を利用できるようになったときに呼び出すデリゲート。

state
Object

コールバックに渡されるユーザー定義の状態オブジェクト。

戻り値

操作の状態を表す IAsyncResult

注釈

指定された DataServiceQueryContinuation<T> オブジェクトには URI が含まれており、実行すると、クエリ結果のデータの次のページが返されます。

適用対象

BeginExecute<TElement>(Uri, AsyncCallback, Object)

サービスからの結果の待機中にこの呼び出しによって処理がブロックされないように、要求を非同期的に送信します。

public:
generic <typename TElement>
 IAsyncResult ^ BeginExecute(Uri ^ requestUri, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginExecute<TElement> (Uri requestUri, AsyncCallback callback, object state);
member this.BeginExecute : Uri * AsyncCallback * obj -> IAsyncResult
Public Function BeginExecute(Of TElement) (requestUri As Uri, callback As AsyncCallback, state As Object) As IAsyncResult

型パラメーター

TElement

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

パラメーター

requestUri
Uri

クエリ要求が送信される URI。 この URI には有効なデータ サービス URI を指定でき、$ クエリ パラメーターを含めることができます。

callback
AsyncCallback

クライアントが結果を利用できるようになったときに呼び出すデリゲート。

state
Object

コールバックに渡されるユーザー定義の状態オブジェクト。

戻り値

非同期操作の状態を追跡するために使用されるオブジェクト。

次の例では、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

注釈

返される IAsyncResult オブジェクトは、非同期操作が完了したタイミングを判断するために使用されます。 詳細については、「非同期操作」をご覧ください。

BeginExecute メソッドは Execute メソッドと同じセマンティクスを使用しますが、サービスからの結果の待機中にこの呼び出しによって処理がブロックされないように、要求を非同期的に送信します。 標準の Begin/End 非同期パターンに従って、クエリ結果が取得されたときに、指定されたコールバックが呼び出されます。

こちらもご覧ください

適用対象