DataServiceContext.BeginExecute Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Skickar asynkront en begäran till datatjänsten om att köra en specifik URI.
Överlagringar
| Name | Description |
|---|---|
| BeginExecute<T>(DataServiceQueryContinuation<T>, AsyncCallback, Object) |
Skickar asynkront en begäran till datatjänsten för att hämta nästa sida med data i ett sidigt frågeresultat. |
| BeginExecute<TElement>(Uri, AsyncCallback, Object) |
Skickar begäran asynkront så att det här anropet inte blockerar bearbetningen i väntan på resultatet från tjänsten. |
BeginExecute<T>(DataServiceQueryContinuation<T>, AsyncCallback, Object)
Skickar asynkront en begäran till datatjänsten för att hämta nästa sida med data i ett sidigt frågeresultat.
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
Typparametrar
- T
Den typ som returneras av frågan.
Parametrar
- continuation
- DataServiceQueryContinuation<T>
Ett DataServiceQueryContinuation<T> objekt som representerar nästa sida med data som ska returneras från datatjänsten.
- callback
- AsyncCallback
Delegera för att anropa när resultat är tillgängliga för klientförbrukning.
- state
- Object
Användardefinierat tillståndsobjekt som skickas till återanropet.
Returer
En IAsyncResult som representerar åtgärdens status.
Kommentarer
Det angivna DataServiceQueryContinuation<T> objektet innehåller den URI som returnerar nästa sida med data i frågeresultatet när det körs.
Gäller för
BeginExecute<TElement>(Uri, AsyncCallback, Object)
Skickar begäran asynkront så att det här anropet inte blockerar bearbetningen i väntan på resultatet från tjänsten.
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
Typparametrar
- TElement
Den typ som returneras av frågan.
Parametrar
- requestUri
- Uri
Den URI som frågebegäran ska skickas till. URI:n kan vara en giltig URI för datatjänsten. den kan innehålla $ frågeparametrar.
- callback
- AsyncCallback
Delegera för att anropa när resultat är tillgängliga för klientförbrukning.
- state
- Object
Användardefinierat tillståndsobjekt som skickas till återanropet.
Returer
Ett objekt som används för att spåra status för den asynkrona åtgärden.
Exempel
I följande exempel visas hur du kör en asynkron fråga genom att anropa BeginExecute metoden för att starta frågan. Det infogade ombudet EndExecute anropar metoden för att visa frågeresultatet. I det här exemplet används det DataServiceContext som genereras av verktyget Lägg till tjänstreferens baserat på Northwind-datatjänsten, som skapas när du slutför 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
Kommentarer
Det returnerade IAsyncResult objektet används för att avgöra när den asynkrona åtgärden har slutförts. Mer information finns i Asynkrona åtgärder.
Metoden BeginExecute använder samma semantik som Execute, men den här metoden skickar asynkront begäran så att det här anropet inte blockerar bearbetningen i väntan på resultatet från tjänsten. Enligt standardmönstret för asynkron start-end anropas det angivna återanropet när frågeresultat hämtas.