我正在使用 QueryOperationResponse GetContinuation() 方法尝试翻阅从 odata wcf 提要读取的非常大的数据集。我正在使用从微软获得的一些 OData 帮助程序库。
问题是分页过程似乎陷入了无限循环,永远不会结束。例如,如果我将页面大小设置为 10000 条记录,并且有 80000 条记录要检索,我观察到循环会继续进行 8 次以上的迭代,此时它应该结束。
下面是查询服务和实现分页的类(在底部)。我还观察到 NextLinkUri 的“OriginalString”在每次迭代中都不会改变,我认为这是错误的?希望我只是遗漏了一些非常明显的东西,我认为这是正确的分页方式:
private static IList<dynamic> Get(string serviceUri, NameValueCollection queryOptions, IAuthenticationScheme authenticationScheme)
{
string baseUri;
string entitySet;
string entityKey;
string queryString;
ValidateServiceUri(serviceUri, out baseUri, out entitySet, out entityKey, out queryString);
string resource = !string.IsNullOrEmpty(entityKey) ? entitySet + "(" + entityKey + ")" : entitySet;
DataServiceContext context = new DataServiceContext(new Uri(baseUri));
context.IgnoreMissingProperties = true;
DataServiceContextHandler handler = new DataServiceContextHandler(authenticationScheme);
handler.HandleGet(context);
DataServiceQuery<EntryProxyObject> query = context.CreateQuery<EntryProxyObject>(resource);
NameValueCollection options = HttpUtility.ParseQueryString(queryString);
options.Add(queryOptions);
foreach (string key in options.AllKeys)
{
query = query.AddQueryOption(key, options[key]);
}
QueryOperationResponse<EntryProxyObject> response = query.Execute() as QueryOperationResponse<EntryProxyObject>;
IList<dynamic> result;
if (options["$inlinecount"] == "allpages")
{
result = new DynamicEntityCollection(response.TotalCount) as IList<dynamic>;
}
else
{
result = new List<dynamic>();
}
foreach (EntryProxyObject proxy in response)
{
DynamicEntity entity = new DynamicEntity(proxy.Properties);
result.Add(entity);
}
while (response.GetContinuation() != null)
{
DataServiceQueryContinuation<EntryProxyObject> continuation = response.GetContinuation();
QueryOperationResponse<EntryProxyObject> nextResponse = context.Execute<EntryProxyObject>(continuation);
Console.WriteLine("Uri: " + continuation.NextLinkUri.OriginalString);
foreach (EntryProxyObject proxy in nextResponse)
{
DynamicEntity entity = new DynamicEntity(proxy.Properties);
result.Add(entity);
}
}
return result;
}
这是我调用方法的方式:
return OData.Get("http://xxx.x.xxx.xx/MyDataService.svc/MyProducts", "$filter=Index gt 0");
最佳答案
你总是在看第一 react 的延续。这保持不变(原因很明显)。您需要查看代码中 nextResponse 的延续,以确定是否有更多数据要读取并获取延续以获取下一页。上面的代码读取第一页,然后一遍又一遍地读取第二页。
关于c# - 使用 WCF OData 和 GetContinuation() 方法的服务器端分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5425255/