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


     在進行 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<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;
            }

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM