System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead
报错:
System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead
产生原因:
在做 net6 webapi
项目时候,读取Request.body
的stream
流时,使用了同步读取的方法 reader.ReadToEnd()
,而报错的大概意思是不要用同步读取
解决方案:
1、使用异步读取
reader.ReadToEndAsync()
2、依然使用同步读取,但需要在program中做一些配置,允许同步读取
builder.Services.Configure<KestrelServerOptions>(option => option.AllowSynchronousIO = true)
.Configure<IISServerOptions>(option => option.AllowSynchronousIO = true);