【发布时间】:2012-02-28 09:45:50
【问题描述】:
我有一个 AsyncController 和一个主页,用于查询用户的朋友列表并与他们一起使用一些数据库。我为调用外部 Web 服务的任何请求实现了异步操作方法模式。这是处理这种情况的有效方法吗?在高请求量期间,我有时会看到 IIS 线程匮乏,我担心我的嵌套 Async 魔法可能会以某种方式参与其中。
我的主要问题/谈话要点是:
- 在 Async 控制器操作中嵌套 IAsyncResult 异步 Web 请求是否安全?或者这只是将某处的负载加倍?
- 使用 ThreadPool.RegisterWaitForSingleObject 处理长时间运行的 Web 请求超时是否有效,或者这会消耗 ThreadPool 线程并饿死应用程序的其余部分?
- 仅在异步控制器操作中执行同步 Web 请求会更有效吗?
示例代码:
public void IndexAsync()
{
AsyncManager.OutstandingOperations.Increment();
User.GetFacebookFriends(friends => {
AsyncManager.Parameters["friends"] = friends;
AsyncManager.OutstandingOperations.Decrement();
});
}
public ActionResult IndexCompleted(List<Friend> friends)
{
return Json(friends);
}
User.GetFacebookFriends(Action<List<Friend>>) 看起来像这样:
void GetFacebookFriends(Action<List<Friend>> continueWith) {
var url = new Uri(string.Format("https://graph.facebook.com/etc etc");
HttpWebRequest wc = (HttpWebRequest)HttpWebRequest.Create(url);
wc.Method = "GET";
var request = wc.BeginGetResponse(result => QueryResult(result, continueWith), wc);
// Async requests ignore the HttpWebRequest's Timeout property, so we ask the ThreadPool to register a Wait callback to time out the request if needed
ThreadPool.RegisterWaitForSingleObject(request.AsyncWaitHandle, QueryTimeout, wc, TimeSpan.FromSeconds(5), true);
}
QueryTimeout 仅在请求时间超过 5 秒时中止请求。
【问题讨论】:
标签: c# asp.net-mvc asynchronous