2013-03-22 28 views
0
public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      var t1= Test1().Result; 
      return View(); 
     } 

     private async Task<HttpResponseMessage> Test1() 
     { 
      string strUrl = "http://localhost:52033/api/values"; 
      var instanceClient = new HttpClient(); 
      var requestMessage = new HttpRequestMessage(HttpMethod.Get, strUrl); 
      var httpRespons = await instanceClient.SendAsync(requestMessage); 
      return httpRespons; 
     } 
    } 

当我在名为Test1的Index Action中调用Test1()时。结果;当前线程死锁程序会发生无响应,不要带结果调用后缀可以正常运行!在MVC操作中调用任务方法,但当前线程被锁定

+0

这是个问题吗? – ElefantPhace 2013-03-22 03:48:35

+0

总之,你想要发生什么? – 2013-03-28 05:07:33

回答

0

您不应该在ASP.NET上下文中调用ResultWait;其中一个原因是它可能导致死锁,as I explain on my blog

相反,你应该使用await,像这样:

public async Task<ActionResult> Index() 
{ 
    var t1 = await Test1(); 
    return View(); 
} 
+0

好!非常感谢你。 – JasonCheng 2013-03-23 02:55:15

相关问题