【发布时间】:2023-03-15 20:43:01
【问题描述】:
我有一种方法如下,它通过网络服务从数据库中获取大量客户数据。该方法返回客户列表如下:
List<Customer> customers = new List<Customer>();
foreach (CustomerSummary cs in lastModifiedCustomers)
{
customers.Add(customerService.CallService(x => x.GetCustomerByKey(cs.Key, context)));
}
return customers;
如何以异步方式调用上面的webservice?我无法对 Web 服务代码进行任何更改。
实际上,在进行 API 调用时,获取如此庞大的数据会导致超时错误。有没有优化的方法来做到这一点?
【问题讨论】:
-
使用任务类。
-
你使用
Task.Run(()=>customerService.CallService -
@Eldho webservice方法是同步的。在进行上述更改时,我得到错误:错误 CS1503 Argument 1: cannot convert from 'System.Threading.Tasks.Task
' to 'Customer' -
你需要等待类似'var cs=await task.run(()=> yourservice.method(1):'这样的结果
-
这肯定不会改变超时错误。因为在任何情况下服务都有超时设置。如果此超时是您的客户端应用程序超时,那么您可以将其调整为更大的值。
标签: c# asp.net web-services asynchronous synchronous