【.net core】Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.

问题描述

微信支付 支付成功异步通知回调 接收微信post过来的数据,方法如下

System.IO.Stream s = context.Request.Body;
int count = 0;
byte[] buffer = new byte[1024];
StringBuilder builder = new StringBuilder();
while ((count = s.Read(buffer, 0, 1024)) > 0)
{
builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
}
s.Flush();
s.Close();
s.Dispose();

会报上边的错误。

 

解决办法

在方法前加如下代码:

var syncIOFeature = context.Features.Get<IHttpBodyControlFeature>();
if (syncIOFeature != null)
{
syncIOFeature.AllowSynchronousIO = true;
}

posted @ 2020-04-16 17:18  游牧人生  阅读(648)  评论(0)    收藏  举报