语言

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 数据服务生成的“添加服务引用”工具,该工具是在完成 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,但此方法异步发送请求,以便在等待服务的结果时,此调用不会阻止处理。 根据标准开始端异步模式,在检索查询结果时调用提供的回调。

另请参阅

适用于