Scene refactoring
Public ActionResult Index (string ucode) { string userInfo = GetUserInfo (ucode). Result; viewdata["UserInfo"] = UserInfo; return View (); } This invokes the deadlock async task<string> GetUserInfo (string ucode) { HttpClient client = new HttpClient (); var httpcontent = new Formurlencodedcontent (new dictionary<string, string> () { {"Ucode", Ucode} }); String uri = "Http://www.xxxx.com/user/get"; var response = await client. Postasync (URI, httpcontent); return response. Content.readasstringasync (). Result; }
Solution Solutions
//other users ' solutions//It's a problem, too. Asynctask<string> GetUserInfo1 (stringUcode) {HttpClient Client=NewHttpClient (); varHttpcontent =NewFormurlencodedcontent (Newdictionary<string,string>() { {"Ucode", Ucode}}); stringURI ="Http://www.xxxx.com/user/get"; varResponse =client. Postasync (URI, httpcontent). Result.Content.ReadAsStringAsync (). Result; returnresponse; } //Final Solution stringGetUserInfo2 (stringUcode) {HttpClient Client=NewHttpClient (); varHttpcontent =NewFormurlencodedcontent (Newdictionary<string,string>() { {"Ucode", Ucode}}); stringURI ="Http://www.xxxx.com/user/get"; varResponse = client. Postasync (URI, httpcontent). Configureawait (false). Getawaiter (). GetResult (). Content.readasstringasync (). Configureawait (false). Getawaiter (). GetResult (); returnresponse; }
Reference URL: https://stackoverflow.com/questions/10004697/calling-configureawait-from-an-asp-net-mvc-action
The story of thread deadlock in async, await in asp.net[MVC]