这两天公众号项目上线,刚开始项目运行没什么问题,但几天之后,访问量激增,服务器崩溃了,每次请求都返回502,一脸懵逼,无从下手,赶紧开日志里的BUG,拿出来一个个改,BUG都改完之后,没有明显的效果,赶紧网上找资料,找了很多找到了这个罪魁祸首,就是Task.Result,原因如下。

IIS连接池连接数有限,高并发的情况下多余的请求会处于排队状态,特别是Task.Result会阻塞请求线程,造成了IIS线程池的资源浪费,解决方案,使用异步接口async/await,这样会很快的释放请求线程,节约资源。

例如:

        [HttpGet, HttpAuth(false)]
public async Task<IActionResult> GetWeChatInfo(string code)
{ #region 获取openid
string 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 = , 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 = , language = "", city = "", province = "", country = "", headimgurl = "", privilege = new List<dynamic>() });
#endregion #region 根据openid获取是否关注公众号
string 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); }

参考链接:

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

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

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

.net core Task.Result Wait等造成502的更多相关文章

  1. git clone error: RPC failed; result=22, HTTP code = 502

    http://www.jianshu.com/p/645d3fe4e028 git克隆的工程太大用https的方式会有如下问题 hbl:tmp hubert$ git clone https://gi ...

  2. spring异步执行报异常No qualifying bean of type 'org.springframework.core.task.TaskExecutor' available

    最近观察项目运行日志的时候突然发现了一个异常, [2018-04-03 10:49:07] 100.0.1.246 http-nio-8080-exec-9 DEBUG org.springframe ...

  3. 初步学习async/await,Task.GetAwaiter,Task.Result

    网上关于async/await的知识有很多,看了很多但不如自己实践一遍来得快,所以这里记录下我的理解和大家学习下. 首先以最简单的同步方法来开始如下 private static void Test( ...

  4. async await task.Result 卡死

    在如下代码中: public async Task<string> GetData() { return await DoWork(); } 在UI线程中调用 var data = Get ...

  5. async,await与task.wait()或task.Result的区别

    你是否曾经与我一样不理解async,await与task.wait()或者task.Result的区别? 接下来,一个Demo让你看出他们之间的区别. static void Main(string[ ...

  6. Task.Result跟 Task.GetAwaiter.GetResult()相同吗?怎么选?

    前几天在用线程池执行一些任务时运到一种情形,就是回调方法中使用到了异步方法,但是回调方法貌似不支持async await的写法.这时候我应该如何处理呢?是使用Task.Result来获取返回结果,还是 ...

  7. 一句 Task.Result 就死锁, 这代码还怎么写?

    一:背景 1. 讲故事 前些天把 .NET 高级调试 方面的文章索引到 github 的过程中,发现了一个有意思的评论,详见 文章,截图如下: 大概就是说在 Winform 的主线程下执行 Task. ...

  8. [转].NET 4.5+项目迁移.NET Core的问题记录 HTTP Error 502.5

    本文转自:http://www.cnblogs.com/ronli/p/5900001.html 这几天试着把目前的开发框架迁移到新的.net core平台,中间遇到的问题在这里简单记录一下. 迁移过 ...

  9. Asp.net Core IIS上安装部署及502.5错误解决

    总结: 安装Microsoft Visual C++ 2015 Redistributable(https://www.microsoft.com/en-us/download/details.asp ...

随机推荐

  1. codeforce 985C Liebig's Barrels(贪心+思维)

    Liebig's Barrels time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. 杂项-EMS:CRM

    ylbtech-杂项-EMS:CRM CMS是"Content Management System"的缩写,意为"内容管理系统". 内容管理系统是企业信息化建设 ...

  3. easyui-combotree 只能选叶子未级

    easyui-combotree 只能选叶子未级 function edit_dg() { //选中一行,获取这一行的属性的值 var selected = $('#tbClientListBrows ...

  4. fullCalendar动态获取数据

    fullCalendar http://fullcalendar.io/docs/event_data/events_function $('#calendar').fullCalendar({ he ...

  5. Py修行路 python基础 (七)文件操作 笔记(随时更改添加)

    文件操作流程: 1.打开文件 open() 2.操作文件 read .writeread(n) n对应读指定个数的 2.x中读取的是字节! 3.x中读取的是字符!read 往外读取文件,是以光标位置开 ...

  6. js实现可拖动的布局

    思路:采用flex布局,js即时修改固定列的宽度 注意:父元素需设置position:relative:因offsetLeft和offsetTop是相对于具有定位的(position:absolute ...

  7. Oracle 10g RAC 如何配置 VIP IPMP

    metalink note 283107.1介绍了如何设置VIP的IPMP,此处记录一下设置过程. o Existing 10g RAC installation ^^^^^^^^^^^^^^^^^^ ...

  8. leetcode144

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

  9. Delphi.NET

    Delphi.NET Borland.VclDSnap.dll Borland.Vcl.TCustomClientDataSet TFieldType.ftBCD Borland.Vcl.TField ...

  10. uboot启动正常,加载内核kernel启…

    先说现象吧:uboot能够正常启动,不过在kernel启动时却出现起不了的现象,停在这里 Uncompressing Linux.................................... ...