关于多线程:ASP.NET Web Service 在另一个线程中的调用

ASP.NET Web Service call in another Thread

我们正在对一些数据更新进行网络服务调用,以同步另一个数据库。此 Web 服务调用会占用一些响应时间。将它添加到线程中会有所帮助吗?这样做有什么坏处吗?如果 Web 服务调用失败,它就会失败,仅此而已。这就像一把火忘记电话。


您可以通过异步回调使用异步 Web 服务调用来防止主线程阻塞。

By making an asynchronous call to a Web service, you can continue to
use the calling thread while you wait for the Web service to respond.
This means users can continue to interact with your application
without it locking up while the Web service access proceeds.

来自 MSDN:进行异步 Web 服务调用


除了 Tudor 的回答之外,我建议您从使用 .NET 4.0.from 任务并行库中的新任务类开始。例如:

1
2
3
4
Task backgroundProcess = new Task(() =>
{
     service.CallMethod();
});

如果挂起用户界面的时间足够长,那么建议在另一个线程上调用它。


我强烈建议不要从 Web 应用程序中使用异步 Web 服务调用(包括在单独的线程中进行调用)。而是使用 Ajax 之类的替代方法,并从 Ajax Call 实例进行此 Web 服务调用。在 Web 上下文中没有简单的方法来处理线程和异步调用。