Redigera

Dela via


DataServiceContext.BeginExecute Method

Definition

Asynchronously sends a request to the data service to execute a specific URI.

Overloads

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

Asynchronously sends a request to the data service to retrieve the next page of data in a paged query result.

BeginExecute<TElement>(Uri, AsyncCallback, Object)

Asynchronously sends the request so that this call does not block processing while waiting for the results from the service.

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

Asynchronously sends a request to the data service to retrieve the next page of data in a paged query result.

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

Type Parameters

T

The type returned by the query.

Parameters

continuation
DataServiceQueryContinuation<T>

A DataServiceQueryContinuation<T> object that represents the next page of data to return from the data service.

callback
AsyncCallback

Delegate to invoke when results are available for client consumption.

state
Object

User-defined state object passed to the callback.

Returns

An IAsyncResult that represents the status of the operation.

Remarks

The supplied DataServiceQueryContinuation<T> object contains the URI that, when executed, returns the next page of data in the query result.

Applies to

BeginExecute<TElement>(Uri, AsyncCallback, Object)

Asynchronously sends the request so that this call does not block processing while waiting for the results from the service.

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

Type Parameters

TElement

The type returned by the query.

Parameters

requestUri
Uri

The URI to which the query request will be sent. The URI may be any valid data service URI; it can contain $ query parameters.

callback
AsyncCallback

Delegate to invoke when results are available for client consumption.

state
Object

User-defined state object passed to the callback.

Returns

An object that is used to track the status of the asynchronous operation.

Examples

The following example shows how to execute an asynchronous query by calling the BeginExecute method to start the query. The inline delegate calls the EndExecute method to display the query results. This example uses the DataServiceContext generated by the Add Service Reference tool based on the Northwind data service, which is created when you complete the 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

Remarks

The returned IAsyncResult object is used to determine when the asynchronous operation has completed. For more information, see Asynchronous Operations.

The method BeginExecute uses the same semantics as Execute, however this method asynchronously sends the request so that this call does not block processing while waiting for the results from the service. According to the standard begin-end asynchronous pattern, the provided callback is invoked when query results are retrieved.

See also

Applies to