欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net core 3.1 中Synchronous operations are disallowed. Call FlushAsync or set AllowSynchronousIO to true instead

程序员文章站 2022-05-11 09:18:04
在进行 Asp.NetCore.MVC 文件上传时,后台无法正常读取文件流保存,出现:Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead. 查找资料,发现需要 ......

     在进行 asp.netcore.mvc  文件上传时,后台无法正常读取文件流保存,出现:synchronous operations are disallowed. call writeasync or set allowsynchronousio to true instead.

    asp.net core 3.1 中Synchronous operations are disallowed. Call FlushAsync or set AllowSynchronousIO to true instead

 

 

   查找资料,发现需要添加允许条件,才可以; 感谢:

  有三种解决方式:第一种:在处理文件的action 中添加:  

var synciofeature = httpcontext.features.get<ihttpbodycontrolfeature>();
if (synciofeature != null)
{
    synciofeature.allowsynchronousio = true;
}

  第二种:或者在startup.cs 中注册

 public void configureservices(iservicecollection services)
        {
            // if using kestrel:
            services.configure<kestrelserveroptions>(options =>
            {
                options.allowsynchronousio = true;
            });

            // if using iis:
            services.configure<iisserveroptions>(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;
            }