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

刚才使用angularjs向后台post数据时候,发现这个问题,.net core 3.1 读取post过来的数据流的时候被阻止了。大致了解了下:3.0中默认禁用了AllowSynchronousIO,如果想同步读取body则需要在ConfigureServices中配置允许同步读取IO流。

我这边了解的解决方案有了如下两种

1、在读取数据流的Action中添加

1 var syncIOFeature = HttpContext.Features.Get<IHttpBodyControlFeature>();
2 if (syncIOFeature != null)
3 {
4     syncIOFeature.AllowSynchronousIO = true;
5 }

2、在Startup.csConfigureServices方法中添加如下代码:

1 services.Configure<Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions>(x => x.AllowSynchronousIO = true)
2         .Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);

我用的是第二种,第一种没有试,不过根据经验应该没问题,只是每个action都写一遍,,会比较繁琐,所以果断选择了第二种。

原文地址:https://www.cnblogs.com/PrintY/p/13945996.html