The story of thread deadlock in async, await in asp.net[MVC]

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.