Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead

报错:
Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead_第1张图片

System.InvalidOperationException: Synchronous operations are disallowed. 
Call ReadAsync or set AllowSynchronousIO to true instead

产生原因:
在做 net6 webapi 项目时候,读取Request.bodystream流时,使用了同步读取的方法reader.ReadToEnd(),而报错的大概意思是不要用同步读取
解决方案:
1、使用异步读取

reader.ReadToEndAsync()

2、依然使用同步读取,但需要在program中做一些配置,允许同步读取

builder.Services.Configure(option => option.AllowSynchronousIO = true)
                .Configure(option => option.AllowSynchronousIO = true);

你可能感兴趣的:(.NET,c#,.net,.net,core,开发语言)