C# JsonConvert.DeserializeObject,索引超出了数组的边界

C# JsonConvert.DeserializeObject,索引超出了数组的边界,c#,json.net,C#,Json.net,所有这些都源于 之所以在这里发布,是因为我第一次看到SO,什么也没看到,所以我在项目的GitHub页面上发布了 我们目前正在使用jsonvert.SerializeObject和jsonvert.DeserializeObject在客户端和服务器之间发送数据 我创建了一个工具,可以创建10个客户端,向10个不同的服务器发送命令,服务器序列化响应并通过网络发送回,然后10个客户端在本地计算机上反序列化对象 我在线程池中同时运行这10个任务,大约20%的时间所有JsonConvert.Deseri

所有这些都源于

之所以在这里发布,是因为我第一次看到SO,什么也没看到,所以我在项目的GitHub页面上发布了


我们目前正在使用
jsonvert.SerializeObject
jsonvert.DeserializeObject
在客户端和服务器之间发送数据

我创建了一个工具,可以创建10个客户端,向10个不同的服务器发送命令,服务器序列化响应并通过网络发送回,然后10个客户端在本地计算机上反序列化对象

我在线程池中同时运行这10个任务,大约20%的时间所有
JsonConvert.DeserializeObject
调用都会因以下堆栈跟踪而失败:

Error: Index was outside the bounds of the array.
at System.Collections.Generic.List1.Add(T item)
at System.Collections.Generic.List1.System.Collections.IList.Add(Object item)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at MyClientCode()
MyClientCode()
正在使用
反序列化对象
如下:

string json = GetServerResponse();
return JsonConvert.DeserializeObject<ResponseObject>(json);
class Obj {
    static List<string> _validSelections = new List<string>() { "One", "Two", "Three", "Four" };
    public IEnumerable<string> ValidSelections { get { return _validSelections; } }
    ... more ...
}
string json=GetServerResponse();
返回JsonConvert.DeserializeObject(json);
ResponseObject
相当大,包含几个复合对象。但是,我可以保存json并使用
反序列化对象
正确地反序列化它,因此我认为对象结构不是问题所在


对列表错误进行一些研究表明,在试图同时修改列表对象时会发生这种情况。

来自詹姆斯·牛顿·金:

每次启动时都会创建一个新的JsonSerializerInternalReader 反序列化对象。每个反序列化都在其自己的状态中进行。 反序列化传入JSON的大容量服务器 在没有问题的情况下同时反序列化许多内容

我猜您有多个反序列化程序在同一个服务器上工作 名单


谢谢你,詹姆斯。深入挖掘之后,我发现您是对的,我们对反序列化类型的多个实例使用了相同的列表对象。具体地说,该对象看起来如下所示:

string json = GetServerResponse();
return JsonConvert.DeserializeObject<ResponseObject>(json);
class Obj {
    static List<string> _validSelections = new List<string>() { "One", "Two", "Three", "Four" };
    public IEnumerable<string> ValidSelections { get { return _validSelections; } }
    ... more ...
}
类Obj{
静态列表_validSelections=新列表(){“一”、“二”、“三”、“四”};
公共IEnumerable ValidSelections{get{return}
更多
}
当试图同时向列表添加对象时,在JsonSerializerInternalReader.cs的第1261行引发异常

在我们的代码中看到这是如何实现的之后,我将摆脱静态支持,因为它并没有为我们提供任何东西