【.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;
}
愿山河浩荡,遍地开花

浙公网安备 33010602011771号