asp.net-mvc – 在ASP.NET MVC中未调用WebClient异步回调
在GET请求我运行(类似):
public ActionResult Index(void) { webClient.DownloadStringComplete += onComplete; webClient.DownloadStringAsync(...); return null; } 我发现直到Index()完成执行后才会调用onComplete. 问题:为什么会发生这种情况?为什么webClient的异步线程显然被阻塞,直到请求处理线程完成? 有没有办法解决这个问题,而无需从ThreadPool启动新线程(我试过这个,并且使用线程池确实按预期工作.如果从ThreadPool的线程调用DownloadStringAsync,webClient的回调确实按预期发生). ASP.NET MVC 3.0,.NET 4.0,MS Cassini开发Web服务器(VS 2010) 编辑:这是一个完整的代码: public class HomeController : Controller { private static ManualResetEvent done; public ActionResult Index() { return Content(DownloadString() ? "success" : "failure"); } private static bool DownloadString() { try { done = new ManualResetEvent(false); var wc = new WebClient(); wc.DownloadStringCompleted += (sender,args) => { // this breakpoint is not hit until after Index() returns. // It is weird though,because response isn't returned to the client (browser) until this callback finishes. // Note: This thread is different from one Index() was running on. done.Set(); }; var uri = new Uri(@"http://us.battle.net/wow/en/character/blackrock/hunt/simple"); wc.DownloadStringAsync(uri); var timedout = !done.WaitOne(3000); if (timedout) { wc.CancelAsync(); // if this would be .WaitOne() instead then deadlock occurs. var timedout2 = !done.WaitOne(3000); Console.WriteLine(timedout2); return !timedout2; } return true; } catch (Exception ex) { Console.WriteLine(ex.Message); } return false; } } 解决方法
我很好奇,所以我询问了Microsoft内部的ASP.NET讨论别名,并得到了Levi Broderick的回复:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- SqlServer如何给表添加新的字段以及字段注释
- asp.net – 多线程环境中的文件访问策略(Web App)
- asp.net-mvc – 如何动态添加到ASP.NET MVC RouteTable?
- asp.net – 经典Asp .asp扩展页的处理程序映射在IIS集成管道
- asp.net – 来自masterpage的Html.RenderPartial调用
- KCFinder for ASP.NET
- asp.net-mvc – 如何使用FluentValidation在ClientSide中验
- ASP.NET API版本控制
- asp.net – 如何在控件中显示十进制值?
- asp.net – 使用httpModule手动启用压缩
- IIS和ASP.Net Web开发服务器之间的行为差??异?
- asp.net – ValidationGroup的RequiredFieldVali
- ASP.NET核心EF6身份
- asp.net – 将通用模型的子类传递给剃刀视图
- 将本地化的resources.resx文件转换为asp.net MVC
- ASP.NET中的Accounting Style字符串格式
- asp.net-mvc – ASP.NET MVC帐户控制器使用指南?
- asp.net-mvc – 从ASP.NET MVC中采用“aspnet_ …
- asp.net – 如何将数据保存在MVC控制器的内存中?
- 手动升级后,将新的ASP.NET Web Optimization框架