C#REST API GET方法:无法处理大量记录
我正在使用VS2017重建一个C#应用程序。但是,我无法检索超过3993条记录,我需要获得~180K才能将它们插入到平面表(SQL db)中 我认为分页方法设计得不正确,我也不确定如何修复它,如果我添加一个大于4K的数字,它就会崩溃。解决这个问题的最佳方法是什么 这就是我们目前的情况: Helper.cs:C#REST API GET方法:无法处理大量记录,c#,rest,api,get,C#,Rest,Api,Get,我正在使用VS2017重建一个C#应用程序。但是,我无法检索超过3993条记录,我需要获得~180K才能将它们插入到平面表(SQL db)中 我认为分页方法设计得不正确,我也不确定如何修复它,如果我添加一个大于4K的数字,它就会崩溃。解决这个问题的最佳方法是什么 这就是我们目前的情况: Helper.cs: { ItemsDto itemsCollection = null; HttpClient httpClient = new HttpClient(); http
{
ItemsDto itemsCollection = null;
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
var uri = string.Format(@"{0}pageSize={1}&page=1", Constants.REQUEST_ENDPOINT, Constants.MAXVALUE);//Constants.MAXVALUE);
uri = uri + "&advancedFilter=__Status__~eq~'Active'";
uri = uri + @"&dynamicColumns=Name,ID,Date,Status,Version,Name,Division";
uri = uri + "&orderDirection=DESC";
HttpResponseMessage response = await httpClient.GetAsync(uri);
if (response == null)
{
throw new Exception("There is no returned request from API");
}
if (response.IsSuccessStatusCode)
{
var responseAsString = await response.Content.ReadAsStringAsync();
itemsCollection = JsonConvert.DeserializeObject<ItemsDto>(responseAsString) ?? new ItemsDto();
}
else
{
throw new Exception("An error request has ocurred: " + response.RequestMessage);
}
if (itemsCollection.Items == null)
{
log.Info("There is no files available");
itemsCollection.Items = new List<Document>();
}
return itemsCollection.Items;
}
<add key="IntMaxValue" value="4000" /> //Change pageSize Here
public static readonly int MAXVALUE = int.Parse(ConfigurationManager.AppSettings["IntMaxValue"]);
最终的解决方案是使用“for”,以便正确使用api分页。在本例中,如果要检索150K条记录,则page=10,pageSize=15000(每页15k)
for(int pagina=1;pagina最终的解决方案是使用“for”来正确使用api分页。在这种情况下,如果我想检索150K记录,那么page=10,pageSize=15000(每页15k)
for(int-pagina=1;pagina)可能您没有足够的内存一次处理那么多数据?当它崩溃时会出现什么错误?谢谢@Steven B这可能是超时问题,我将研究一下如何配置它。@Mikael错误是“任务被取消。内部异常”我将尝试逐行调试代码,看看是否可以看到其他内容。@tana检查关于该错误的答案。可能是超时了。可能是因为您没有足够的内存一次处理那么多代码?当它崩溃时,会出现什么错误?谢谢@Steven B可能是超时问题,我将稍微调查一下,看看如何配置e那个。@Mikael错误是“一个任务被取消了。内部异常”我将尝试逐行调试代码,看看是否还能看到其他东西。@tana检查关于那个错误的答案。它可能超时了
for(int pagina=1; pagina<=10; pagina++)
{
HttpClient httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromMinutes(30);
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
var uri = string.Format(@"{0}pageSize={1}&page={2}", Constants.REQUEST_ENDPOINT, 15000,pagina);//Constants.MAXVALUE);
uri = uri + "&advancedFilter=__Status__~eq~'Active'";
uri = uri + @"&dynamicColumns=Name,ID,Date,Status,Version,Name,Division";
uri = uri + "&orderDirection=DESC";
HttpResponseMessage response = await httpClient.GetAsync(uri);
if (response == null)
{
throw new Exception("There is no returned request from TAP API");
}
if (response.IsSuccessStatusCode)
{
var responseAsString = await response.Content.ReadAsStringAsync();
itemsCollection = JsonConvert.DeserializeObject<ItemsDto>(responseAsString) ?? new ItemsDto();
itemsCollectionTotal.Add(itemsCollection);
}
else
{
throw new Exception("An error request has ocurred: " + response.RequestMessage);
}
if (itemsCollection.Items == null)
{
log.Info("There is no documents available from API");
itemsCollection.Items = new List<Document>();
}
}
return itemsCollectionTotal;//itemsCollectionTotal.Item;
}