Article From:https://www.cnblogs.com/chenyishi/p/9061090.html

This two days public number project on-line, just start the project operation no problem, but a few days later, the number of visits increased, the server crashed, each request returned to 502, a face muddled, no hand, quickly open the BUG in the log, take out one by one, BUG have been changed, there is no obvious effect, after the change, no obvious effect, no obvious effect,Quickly find information on the Internet, found many found the culprit, that is, Task.Result, for the following reasons.

IISThe number of connection pool connections is limited, and the excess requests will be in queuing condition in the case of high concurrency. In particular, the Task.Result will block the request thread, resulting in the resource waste of the IIS thread pool, the solution, and the asynchronous interface async/await, which will quickly release the request line.Save the resources.

For example:

        [HttpGet, HttpAuth(false)]
        public async Task<IActionResult> GetWeChatInfo(string code)
        {

            #region Get openidstring url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx3e&secret=7c61&code=" + code + "&grant_type=authorization_code";
            string result =await HttpUtil.GetData(url);
            var openInfo = JsonConvert.DeserializeAnonymousType(result, new { access_token = "", refresh_token = "", expires_in = 0, openid = "", scope = "" });

            string urlStr2 = "https://api.weixin.qq.com/sns/userinfo?access_token=" + openInfo.access_token + "&openid=" + openInfo.openid + "&lang=zh_CN";
            string result2 = await HttpUtil.GetData(urlStr2);
            var userinfo = JsonConvert.DeserializeAnonymousType(result2, new { openid = "", nickname = "", sex = 1, language = "", city = "", province = "", country = "", headimgurl = "", privilege = new List<dynamic>() });
            #endregion

            #region Gain attention to public number according to openidstring urlA = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3&secret=7c6";
            string resultA = await HttpUtil.GetData(urlA);
            var openInfoA = JsonConvert.DeserializeAnonymousType(resultA, new { access_token = "" });

            string urlStrB = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + openInfoA.access_token + "&openid=" + userinfo.openid + "&lang=zh_CN";
            string resultC = await HttpUtil.GetData(urlStrB);
            #endregion

            var userInfoJson = JObject.Parse(resultC);
            return Success(userInfoJson);

        }

 

Reference links:

https://q.cnblogs.com/q/106342/

https://www.cnblogs.com/farb/p/5014773.html

https://www.cnblogs.com/vipyoumay/p/5663950.html

 

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *