Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Newtonsoft.Json System.InvalidOperationException:不允许同步操作_C#_Asp.net Core_Json.net - Fatal编程技术网

C# Newtonsoft.Json System.InvalidOperationException:不允许同步操作

C# Newtonsoft.Json System.InvalidOperationException:不允许同步操作,c#,asp.net-core,json.net,C#,Asp.net Core,Json.net,我得到以下异常,我不知道如何解决它。我不想设置AllowSynchronousIO=true。MS从3.0开始做出决定,默认情况下不再允许同步,这是有充分理由的。那么我该如何回避这个问题呢 System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead. at Microsoft.AspNetCo

我得到以下异常,我不知道如何解决它。我不想设置
AllowSynchronousIO=true
。MS从3.0开始做出决定,默认情况下不再允许同步,这是有充分理由的。那么我该如何回避这个问题呢

System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.StreamReader.ReadBuffer(Span`1 userBuffer, Boolean& readToUserBuffer)
   at System.IO.StreamReader.ReadSpan(Span`1 buffer)
   at System.IO.StreamReader.Read(Char[] buffer, Int32 index, Int32 count)
   at Newtonsoft.Json.JsonTextReader.ReadData(Boolean append, Int32 charsRequired)
   at Newtonsoft.Json.JsonTextReader.ParseValue()
   at Newtonsoft.Json.JsonReader.ReadAndMoveToContent()
   at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)

使用
Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream
类作为流的包装器解决了这个问题。这允许您异步访问流,然后将流公开给同步工具(如Newtonsoft.Json),而无需为整个解决方案启用SynchronousIO。MVC也通过使用FileBufferingReadStream类来避免这个错误,因此它经过了良好的测试


或等到/if Json.Net支持反序列化异步。。。(假设您不想自己读取流)如果有效负载足够大,以至于JSON文档无法一次性读取,并且您无法将其缩小,那么我认为您唯一的选择是将
AllowSynchronousIO
设置为
true
(甚至可能只是根据需要的请求),或者删除Newtonsoft.Json的用法,改用System.Text.Json(或其他支持异步的序列化程序)。Github讨论了这一特定更改以及microsoft进行更改的原因。