在进行 asp.netcore.mvc  文件上传时,后台无法正常读取文件流保存,出现:synchronous operations are disallowed. call writeasync 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;
            }