DataServiceContext.EndExecute<TElement> 方法 (IAsyncResult)
呼叫以完成 BeginExecute。
命名空間: System.Data.Services.Client
組件: Microsoft.Data.Services.Client (在 Microsoft.Data.Services.Client.dll 中)
語法
'宣告
Public Function EndExecute(Of TElement) ( _
asyncResult As IAsyncResult _
) As IEnumerable(Of TElement)
'用途
Dim instance As DataServiceContext
Dim asyncResult As IAsyncResult
Dim returnValue As IEnumerable(Of TElement)
returnValue = instance.EndExecute(asyncResult)
public IEnumerable<TElement> EndExecute<TElement>(
IAsyncResult asyncResult
)
public:
generic<typename TElement>
IEnumerable<TElement>^ EndExecute(
IAsyncResult^ asyncResult
)
member EndExecute :
asyncResult:IAsyncResult -> IEnumerable<'TElement>
JScript 不支援泛型型別及方法。
型別參數
- TElement
查詢所傳回的型別。
參數
- asyncResult
型別:System.IAsyncResult
IAsyncResult 物件
傳回值
型別:System.Collections.Generic.IEnumerable<TElement>
查詢作業所傳回的結果。
例外狀況
例外狀況 | 條件 |
---|---|
ArgumentNullException | 當 asyncResult 為 nullnull 參考 (在 Visual Basic 中為 Nothing) 時。 |
ArgumentException | 當 asyncResult 不是來自這個 DataServiceContext 執行個體時。 -或- 當 EndExecute 方法先前已呼叫時。 |
InvalidOperationException | 在執行要求期間或將回應訊息的內容轉換為物件時引發錯誤時。 |
DataServiceQueryException | 當資料服務傳回「HTTP 404:找不到資源」錯誤時。 |
備註
根據標準的開始-結束非同步模式,在擷取查詢結果時會叫用所提供的回呼。 如需詳細資訊,請參閱非同步作業 (WCF Data Services)。
叫用回呼的時候,就會已經從 HTTP 資料流讀取所有結果,但是未加以處理,而且不會對任何本機使用者端的物件進行具體化或修改,也不會發生識別解析。 叫用 EndExecute 的時候,就會建立並傳回 DataServiceResponse,但是仍未處理結果。 只有當使用者列舉結果的時候,才會發生識別解析、物件具體化和管理。
範例
下列範例示範如何透過呼叫 BeginExecute 方法啟動查詢來執行非同步查詢。 內嵌委派會呼叫 EndExecute 方法以顯示查詢結果。 此範例使用「加入服務參考」工具所產生的 DataServiceContext,並根據完成 WCF Data Services 快速入門時所建立的 Northwind 資料服務。
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
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);
}
}
}
請參閱
參考
System.Data.Services.Client 命名空間