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;
        }
    }

当我在Index Action 中调用Test1()。结果; 当当前线程程序一直没有响应时会发生死锁,不带Result调用后缀就可以正常运行!

4

1 回答 1

0

您不应该在 ASP.NET 上下文中调用Resultor ;Wait一个原因是它可能导致死锁,正如我在博客中解释的那样

相反,您应该使用await,如下所示:

public async Task<ActionResult> Index()
{
    var t1 = await Test1();
    return View();
}
于 2013-03-22T12:32:32.877 回答