DataServiceContext.EndExecute<TElement>(IAsyncResult) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
public:
generic <typename TElement>
System::Collections::Generic::IEnumerable<TElement> ^ EndExecute(IAsyncResult ^ asyncResult);
public System.Collections.Generic.IEnumerable<TElement> EndExecute<TElement>(IAsyncResult asyncResult);
member this.EndExecute : IAsyncResult -> seq<'Element>
Public Function EndExecute(Of TElement) (asyncResult As IAsyncResult) As IEnumerable(Of TElement)
類型參數
- TElement
查詢回傳的類型。
參數
- asyncResult
- IAsyncResult
IAsyncResult 物件
傳回
查詢操作回傳的結果。
例外狀況
當 asyncResult 是 null。
當錯誤發生時,無論是在請求執行過程中,還是當回應訊息內容轉換成物件時。
範例
以下範例展示了如何透過呼叫 BeginExecute 方法來啟動查詢來執行非同步查詢。 內嵌代理呼叫 EndExecute 該方法來顯示查詢結果。 此範例使用由 Add Service Reference 工具產生的, 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
備註
根據標準的開始-結束非同步模式,當查詢結果被檢索時會呼叫所提供的回撥。 欲了解更多資訊,請參閱 非同步操作。
當呼叫回用時,所有結果都已從 HTTP 串流讀取,但尚未被處理;尚未實現或修改本地使用者物件,身份解析也未發生。 當 EndExecute 被呼叫時,會建立並回傳 a DataServiceResponse ,但結果仍未被處理。 身份解析、物件實體化與操作僅在使用者列舉結果時發生。