2011-03-24 52 views
2

我正在使用QueryOperationResponse GetContinuation()方法來嘗試並通過讀取odata wcf訂閱源的非常大的數據集來翻頁。我正在使用從Microsoft獲得的一些OData輔助程序庫。使用WCF OData和GetContinuation()方法進行服務器端分頁

問題是,分頁過程似乎卡在一個無限循環,並永遠不會結束。例如,如果我將頁面大小設置爲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"); 

回答

4

你一直在尋找的延續第一個迴應。這保持不變(顯而易見的原因)。您需要查看代碼中nextResponse的延續情況,以確定是否有更多數據需要讀取,並繼續獲取下一頁。上面的代碼讀取第一頁,然後一遍又一遍讀取第二頁。