DataServiceContext.Execute<TElement> 方法 (Uri)
通过将请求发送到数据服务来执行特定 URI。
用于 Silverlight 的 WCF Data Services 5.0 客户端不支持。
命名空间: System.Data.Services.Client
程序集: Microsoft.Data.Services.Client(在 Microsoft.Data.Services.Client.dll 中)
语法
声明
Public Function Execute(Of TElement) ( _
requestUri As Uri _
) As IEnumerable(Of TElement)
用法
Dim instance As DataServiceContext
Dim requestUri As Uri
Dim returnValue As IEnumerable(Of TElement)
returnValue = instance.Execute(requestUri)
public IEnumerable<TElement> Execute<TElement>(
Uri requestUri
)
public:
generic<typename TElement>
IEnumerable<TElement>^ Execute(
Uri^ requestUri
)
member Execute :
requestUri:Uri -> IEnumerable<'TElement>
JScript 不支持一般类型和方法。
类型参数
- TElement
查询返回的类型。
参数
- requestUri
类型:System.Uri
查询请求将发送到的 URI。该 URI 可以是任何有效的数据服务 URI。可以包含 $ 查询参数。
返回值
类型:System.Collections.Generic.IEnumerable<TElement>
查询操作的结果。
异常
异常 | 条件 |
---|---|
WebException | 当没有收到发往 requestUri 的请求的响应时。 |
ArgumentNullException | 当 requestUri 为 nullnull 引用(在 Visual Basic 中为 Nothing) 时。 |
ArgumentException | 当 requestUri 不是数据服务的有效 URI 时。 |
InvalidOperationException | 在执行请求期间或在请求将响应消息的内容转换为对象期间引发错误时。 |
DataServiceQueryException | 当数据服务返回“HTTP 404: 未找到资源”错误时。 |
注释
Execute 方法用于通过 URI 查询数据服务;该方法会导致将 HTTP GET 请求发送给数据服务。 指定的请求 URI 可为绝对值或相对值。
如果 requestUri 为绝对值,则此方法用于验证 URI 是否指向构造 DataServiceContext 时所指定的数据服务。 如果 requestUri 为相对值,则此方法会将构造 DataServiceContext 时所提供的内容与任何前导斜杠剥离并对其追加 requestUri。 将在传递给 DataServiceContext 构造函数的 URI 后面追加斜杠(如果斜杠不存在)。
此方法返回时,已从网络流读取请求的所有 HTTP 响应,但尚未对响应进行处理;没有标识解析或对象具体化。 在枚举响应中的指定实体之前,尚未对该实体进行标识解析或完全对象具体化操作。
示例
以下示例使用 do?while 循环从数据服务的分页结果中加载 Customers 实体。 通过使用下一链接 URI 调用 Execute 方法来接收下一页数据。
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
Dim token As DataServiceQueryContinuation(Of Customer) = Nothing
Dim pageCount = 0
Try
' Execute the query for all customers and get the response object.
Dim response As QueryOperationResponse(Of Customer) = _
CType(context.Customers.Execute(), QueryOperationResponse(Of Customer))
' With a paged response from the service, use a do...while loop
' to enumerate the results before getting the next link.
Do
' Write the page number.
Console.WriteLine("Page {0}:", pageCount + 1)
' If nextLink is not null, then there is a new page to load.
If token IsNot Nothing Then
' Load the new page from the next link URI.
response = CType(context.Execute(Of Customer)(token), _
QueryOperationResponse(Of Customer))
End If
' Enumerate the customers in the response.
For Each customer As Customer In response
Console.WriteLine(vbTab & "Customer Name: {0}", customer.CompanyName)
Next
' Get the next link, and continue while there is a next link.
token = response.GetContinuation()
Loop While token IsNot Nothing
Catch ex As DataServiceQueryException
Throw New ApplicationException( _
"An error occurred during query execution.", ex)
End Try
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
DataServiceQueryContinuation<Customer> token = null;
int pageCount = 0;
try
{
// Execute the query for all customers and get the response object.
QueryOperationResponse<Customer> response =
context.Customers.Execute() as QueryOperationResponse<Customer>;
// With a paged response from the service, use a do...while loop
// to enumerate the results before getting the next link.
do
{
// Write the page number.
Console.WriteLine("Page {0}:", pageCount++);
// If nextLink is not null, then there is a new page to load.
if (token != null)
{
// Load the new page from the next link URI.
response = context.Execute<Customer>(token)
as QueryOperationResponse<Customer>;
}
// Enumerate the customers in the response.
foreach (Customer customer in response)
{
Console.WriteLine("\tCustomer Name: {0}", customer.CompanyName);
}
}
// Get the next link, and continue while there is a next link.
while ((token = response.GetContinuation()) != null);
}
catch (DataServiceQueryException ex)
{
throw new ApplicationException(
"An error occurred during query execution.", ex);
}
请参阅
参考
System.Data.Services.Client 命名空间