私は、AsyncControllerと、ユーザーのフレンドリストを照会するホームページを持っています。私は、外部Webサービスを呼び出す要求に対して非同期アクションメソッドパターンを実装しました。これはこの状況を効率的に処理する方法ですか?要求量が多い時間に、私は時々スレッドが枯渇しているのを目の当たりにしています。私は入れ子にされたAsyncの魔法が何らかの形でこれに関わっているかもしれないと心配しています。ASP.NET MVC AsyncControllerとIOバインドされたリクエスト
私の主な質問は、/話のポイントは以下のとおりです。
- それは非同期コントローラのアクション内部たIAsyncResult非同期のWeb要求が巣に安全ですか?または、これはちょうどどこかの負荷を2倍にしていますか?
- 長時間実行されているWeb要求のタイムアウトを処理するにはThreadPool.RegisterWaitForSingleObjectを使用するのが効率的ですか?これはThreadPoolスレッドを食べ、残りのアプリケーションを飢えさせますか?
- Async Controllerアクション内で同期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>>)
は次のようになります。それは5秒以上かかる場合
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だけの要求を中止します。
Oh drat私はStreamReaderのReadToEnd()を使用してStreamを読み込んでいました。ありがとうございました :) – Foritus