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 方法以顯示查詢結果。 此範例會DataServiceContext根據 Northwind 數據服務使用 Add Service Reference 工具所產生的 ,當您完成 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 相同的語意,但是此方法會以非同步方式傳送要求,讓這個呼叫不會在等候來自服務的結果時封鎖處理。 根據標準的開始-結束非同步模式,在擷取查詢結果時會叫用所提供的回呼。

另請參閱

適用於