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


問題說明:

在.Net Core 3.0中進行了很多方面升級,常見錯誤之一:

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

 

出現錯誤的原因:

在.Net Core 3.0版本中,默認同步讀取請求流 為 異步方式,默認不支持同步讀取。

代碼說明:

              using (StreamReader sr = new StreamReader(Request.Body, Encoding.UTF8))
                    {
                        //string content = sr.ReadToEnd();  //.Net Core 3.0 默認不再支持
                        string content = sr.ReadToEndAsync().Result;

 

解決方案:

1.使用異步讀取請求上下文

 string content = sr.ReadToEndAsync().Result;

2.在配置中開啟同步讀取流操作

  //配置可以同步請求讀取流數據
            services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true)
                .Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);

 

更多:

asp.netCore3.0區域和路由配置變化

在Asp.Net Core 3.0中如何使用 Newtonsoft.Json 庫序列化數據  

Asp.Net Core Cookie使用,Asp.net Core Cookie操作失效 

 


免責聲明!

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



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