asp.net core 3.1 中Synchronous operations are disallowed. Call FlushAsync or set AllowSynchronousIO t
在进行 Asp.NetCore.MVC 文件上传时,后台无法正常读取文件流保存,出现:Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead.
查找资料,发现需要添加允许条件,才可以; 感谢:https://www.cnblogs.com/lonelyxmas/p/12060869.html
有三种解决方式:第一种:在处理文件的Action 中添加:
var syncIOFeature = HttpContext.Features.Get(); if (syncIOFeature != null) { syncIOFeature.AllowSynchronousIO = true; }
第二种:或者在Startup.cs 中注册
public void ConfigureServices(IServiceCollection services) { // If using Kestrel: services.Configure(options => { options.AllowSynchronousIO = true; }); // If using IIS: services.Configure (options => { options.AllowSynchronousIO = true; });
}
第三种:(不太理解。。。)
Request.EnableBuffering(); using (var reader = new StreamReader(Request.Body, encoding: Encoding.UTF8)) { var body = reader.ReadToEndAsync(); // Do some processing with body… // Reset the request body stream position so the next middleware can read it Request.Body.Position = 0; }