Share via


DataServiceContext.BeginExecute Metode

Definisi

Secara asinkron mengirim permintaan ke layanan data untuk menjalankan URI tertentu.

Overload

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

Secara asinkron mengirim permintaan ke layanan data untuk mengambil halaman data berikutnya dalam hasil kueri berhala.

BeginExecute<TElement>(Uri, AsyncCallback, Object)

Secara asinkron mengirim permintaan sehingga panggilan ini tidak memblokir pemrosesan saat menunggu hasil dari layanan.

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

Secara asinkron mengirim permintaan ke layanan data untuk mengambil halaman data berikutnya dalam hasil kueri berhala.

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

Jenis parameter

T

Jenis yang dikembalikan oleh kueri.

Parameter

continuation
DataServiceQueryContinuation<T>

Objek DataServiceQueryContinuation<T> yang mewakili halaman data berikutnya untuk dikembalikan dari layanan data.

callback
AsyncCallback

Delegasikan untuk memanggil saat hasil tersedia untuk konsumsi klien.

state
Object

Objek status yang ditentukan pengguna diteruskan ke panggilan balik.

Mengembalikan

IAsyncResult Yang mewakili status operasi.

Keterangan

Objek yang disediakan DataServiceQueryContinuation<T> berisi URI yang, saat dijalankan, mengembalikan halaman data berikutnya dalam hasil kueri.

Berlaku untuk

BeginExecute<TElement>(Uri, AsyncCallback, Object)

Secara asinkron mengirim permintaan sehingga panggilan ini tidak memblokir pemrosesan saat menunggu hasil dari layanan.

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

Jenis parameter

TElement

Jenis yang dikembalikan oleh kueri.

Parameter

requestUri
Uri

URI tempat permintaan kueri akan dikirim. URI dapat berupa URI layanan data yang valid; ini dapat berisi $ parameter kueri.

callback
AsyncCallback

Delegasikan untuk memanggil saat hasil tersedia untuk konsumsi klien.

state
Object

Objek status yang ditentukan pengguna diteruskan ke panggilan balik.

Mengembalikan

Objek yang digunakan untuk melacak status operasi asinkron.

Contoh

Contoh berikut menunjukkan cara menjalankan kueri asinkron dengan memanggil BeginExecute metode untuk memulai kueri. Delegasi sebaris memanggil EndExecute metode untuk menampilkan hasil kueri. Contoh ini menggunakan DataServiceContext yang dihasilkan oleh alat Tambahkan Referensi Layanan berdasarkan layanan data Northwind, yang dibuat saat Anda menyelesaikan Layanan Data 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

Keterangan

Objek yang dikembalikan IAsyncResult digunakan untuk menentukan kapan operasi asinkron telah selesai. Untuk informasi selengkapnya, lihat Operasi Asinkron.

Metode BeginExecute ini menggunakan semantik yang sama dengan Execute, namun metode ini secara asinkron mengirim permintaan sehingga panggilan ini tidak memblokir pemrosesan saat menunggu hasil dari layanan. Menurut pola asinkron awal standar, panggilan balik yang disediakan dipanggil saat hasil kueri diambil.

Lihat juga

Berlaku untuk