异步调用网络服务

Calling a webservice asynchonously

我有一个方法如下,它通过网络服务从数据库中获取大量客户数据。 returns客户列表的方法如下图:

 List<Customer> customers = new List<Customer>();

 foreach (CustomerSummary cs in lastModifiedCustomers)
 {
    customers.Add(customerService.CallService(x => x.GetCustomerByKey(cs.Key, context)));
 }

 return customers;

如何异步调用上面的webservice?我无法对网络服务代码进行任何更改。

实际获取如此庞大的数据会在调用 API 时出现超时错误。有什么优化方法可以做到这一点吗?

尝试这样的事情

int size=1000;
int iterations = (lastModifiedCustomers.Items.Count/size)+1;

List<Customer> customers = new List<Customer>();
for(int i=1;i<iterations;i++)
{

 foreach (CustomerSummary cs in lastModifiedCustomers.GetRange(i==1?i:(i-1)*size+1,size))
 {
    customers.Add(customerService.CallService(x => x.GetCustomerByKey(cs.Key, context)));
 }
}
 return customers;

我使用上面的方法出现了越界错误solution.So我更正如下:

int size=100;
List<Customer> customers = new List<Customer>();
for(int i=0;i<iterations;i+=size)
{
 foreach (CustomerSummary cs in lastModifiedCustomers.GetRange(i,Math.Min(size,lastModifiedCustomers.Count-i)))
 {
    customers.Add(customerService.CallService(x => x.GetCustomerByKey(cs.Key, context)));
 }
}
 return customers;