語言

DataServiceContext.BeginExecute 方法

定義

非同步地向資料服務發送請求,以執行特定的 URI。

多載

名稱 Description
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 該方法來顯示查詢結果。 此範例使用由 Add Service Reference 工具產生的, DataServiceContext 該工具基於 Northwind 資料服務,該服務會在完成 WCF 資料服務時建立。

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但此方法非同步傳送請求,避免在等待服務結果時阻塞處理。 根據標準的開始-結束非同步模式,當查詢結果被檢索時會呼叫所提供的回撥。

另請參閱

適用於