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 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 异步模式,在检索查询结果时将调用所提供的回调。

另请参阅

适用于