Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC异步控制器和IO绑定请求_C#_Asp.net Mvc_Asynchronous - Fatal编程技术网

C# ASP.NET MVC异步控制器和IO绑定请求

C# ASP.NET MVC异步控制器和IO绑定请求,c#,asp.net-mvc,asynchronous,C#,Asp.net Mvc,Asynchronous,我有一个AsyncController和一个主页,可以查询用户的朋友列表,并对他们进行一些数据库工作。我为调用外部web服务的任何请求实现了异步操作方法模式。这是处理这种情况的有效方法吗?在高请求量的时候,我有时会看到IIS线程不足,我担心我的嵌套异步魔法可能与此有关 我的主要问题/谈话要点是: 在异步控制器操作中嵌套IAsyncResult异步web请求安全吗?或者这只是在某个地方增加了一倍的工作量 使用ThreadPool.RegisterWaitForSingleObject处理超时长时

我有一个AsyncController和一个主页,可以查询用户的朋友列表,并对他们进行一些数据库工作。我为调用外部web服务的任何请求实现了异步操作方法模式。这是处理这种情况的有效方法吗?在高请求量的时候,我有时会看到IIS线程不足,我担心我的嵌套异步魔法可能与此有关

我的主要问题/谈话要点是:

  • 在异步控制器操作中嵌套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);
}
public void IndexAsync()
{
AsyncManager.OutstandingOperations.Increment();
User.GetFacebookFriends(friends=>{
AsyncManager.Parameters[“friends”]=朋友;
AsyncManager.OutstandingOperations.Decrement();
});
}
公共行动结果索引已完成(列出好友)
{
返回Json(朋友);
}
User.GetFacebookFriends(操作)
如下所示:

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);
}
void GetFacebookFriends(操作继续){
var url=新Uri(string.Format(“https://graph.facebook.com/etc 等);
HttpWebRequest wc=(HttpWebRequest)HttpWebRequest.Create(url);
wc.Method=“GET”;
var request=wc.BeginGetResponse(结果=>QueryResult(结果,continueWith),wc);
//异步请求忽略HttpWebRequest的Timeout属性,因此如果需要,我们要求线程池注册一个等待回调来超时请求
RegisterWaitForSingleObject(request.AsyncWaitHandle,QueryTimeout,wc,TimeSpan.FromSeconds(5),true);
}

QueryTimeout只会在超过5秒的时间内中止请求。

您首先描述的完全异步方法是最好的,因为这会将TP线程释放回池中以供重用。很可能您正在其他地方执行某些其他阻止操作。在
QueryResponse
中会发生什么?尽管您获取了响应异步读取响应流吗?如果不是,那么TP饥饿应该减少。

噢,德拉特,我只是使用StreamReader的ReadToEnd()读取响应流,那就是了。谢谢:)