SignalR Hub + JSON.NET TypeNameHandling:在JSON'System.Linq.Enumerable WhereSelectEnumerableIterator`2中指定的错误解析类型

时间:2015-03-21 02:14:46

标签: c# asp.net json.net signalr

我正在使用SignalR尝试让.net客户端调用SignalR Hub上的.net方法。我的情况是传输的对象不一定是简单的值类型(string,int,double等);它们可以是DTO,例如泛型<< T>>或其他非平凡的事情。我需要使用与序列化时相同的对象类型对它们进行反序列化。要做到这一点,我相信我需要将JSON.Net TypeNameHandling设置为" All"。我相信我需要在客户端和服务器上都这样做。

在客户端上对TypeNameHandling进行更改似乎运行顺利,但是当我在服务器上进行更改时,在处理任何请求之前,我在连接时在服务器上收到错误。以下是我更改服务器上JSON.Net设置的方法:

class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var serializer = new JsonSerializer() 
        { 
            TraceWriter = new ConsoleTraceWriter(),
            TypeNameHandling = TypeNameHandling.All,
    };

    // register it so that signalr can pick it up
    GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => serializer);

    app.UseCors(CorsOptions.AllowAll);
    app.MapSignalR();
}

使用输出到控制台的自定义TraceWriter,我在客户端上看到以下内容:

01:36:55.8596332 - null - ChangeState(Disconnected, Connecting)
-> Msg: Started serializing System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Collections.Generic.KeyValuePa
ir`2[System.String,Microsoft.AspNet.SignalR.Client.Hubs.HubProxy],Microsoft.AspNet.SignalR.Client.Hubs.HubRegistrationDa
ta]. Path ''.
-> Msg: Writing type name 'System.Linq.Enumerable+WhereSelectEnumerableIterator`2[[System.Collections.Generic.KeyValuePa
ir`2[[System.String, mscorlib],[Microsoft.AspNet.SignalR.Client.Hubs.HubProxy, Microsoft.AspNet.SignalR.Client]], mscorl
ib],[Microsoft.AspNet.SignalR.Client.Hubs.HubRegistrationData, Microsoft.AspNet.SignalR.Client]], System.Core' for Syste
m.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Collections.Generic.KeyValuePair`2[System.String,Microsoft.AspN
et.SignalR.Client.Hubs.HubProxy],Microsoft.AspNet.SignalR.Client.Hubs.HubRegistrationData]. Path ''.
-> Msg: Started serializing Microsoft.AspNet.SignalR.Client.Hubs.HubRegistrationData. Path '$values'.
-> Msg: Writing type name 'Microsoft.AspNet.SignalR.Client.Hubs.HubRegistrationData, Microsoft.AspNet.SignalR.Client' fo
r Microsoft.AspNet.SignalR.Client.Hubs.HubRegistrationData. Path '$values[0]'.
-> Msg: Finished serializing Microsoft.AspNet.SignalR.Client.Hubs.HubRegistrationData. Path '$values[0]'.
-> Msg: Finished serializing System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Collections.Generic.KeyValueP
air`2[System.String,Microsoft.AspNet.SignalR.Client.Hubs.HubProxy],Microsoft.AspNet.SignalR.Client.Hubs.HubRegistrationD
ata]. Path ''.
-> Msg: Serialized JSON:
{
  "$type": "System.Linq.Enumerable+WhereSelectEnumerableIterator`2[[System.Collections.Generic.KeyValuePair`2[[System.St
ring, mscorlib],[Microsoft.AspNet.SignalR.Client.Hubs.HubProxy, Microsoft.AspNet.SignalR.Client]], mscorlib],[Microsoft.
AspNet.SignalR.Client.Hubs.HubRegistrationData, Microsoft.AspNet.SignalR.Client]], System.Core",
  "$values": [
    {
      "$type": "Microsoft.AspNet.SignalR.Client.Hubs.HubRegistrationData, Microsoft.AspNet.SignalR.Client",
      "Name": "MyHub"
    }
  ]
}
01:36:56.4606332 - null - Disconnected
01:36:56.4616332 - null - Transport.Dispose()
01:36:56.4626332 - null - Closed

服务器显示:

-> Msg: Error deserializing System.Collections.Generic.IEnumerable`1[Microsoft.AspNet.SignalR.Hubs.HubDispatcher+ClientH
ubInfo]. Error resolving type specified in JSON 'System.Linq.Enumerable WhereSelectEnumerableIterator`2[[System.Collecti
ons.Generic.KeyValuePair`2[[System.String, mscorlib],[Microsoft.AspNet.SignalR.Client.Hubs.HubProxy, Microsoft.AspNet.Si
gnalR.Client]], mscorlib],[Microsoft.AspNet.SignalR.Client.Hubs.HubRegistrationData, Microsoft.AspNet.SignalR.Client]],
System.Core'. Path '$type', line 1, position 333.
-> ex: Newtonsoft.Json.JsonSerializationException: Error resolving type specified in JSON 'System.Linq.Enumerable WhereS
electEnumerableIterator`2[[System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib],[Microsoft.AspNet.SignalR
.Client.Hubs.HubProxy, Microsoft.AspNet.SignalR.Client]], mscorlib],[Microsoft.AspNet.SignalR.Client.Hubs.HubRegistratio
nData, Microsoft.AspNet.SignalR.Client]], System.Core'. Path '$type', line 1, position 333. ---> Newtonsoft.Json.JsonSer
ializationException: Could not find type 'System.Linq.Enumerable WhereSelectEnumerableIterator`2[[System.Collections.Gen
eric.KeyValuePair`2[[System.String, mscorlib],[Microsoft.AspNet.SignalR.Client.Hubs.HubProxy, Microsoft.AspNet.SignalR.C
lient]], mscorlib],[Microsoft.AspNet.SignalR.Client.Hubs.HubRegistrationData, Microsoft.AspNet.SignalR.Client]]' in asse
mbly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
   at Newtonsoft.Json.Serialization.DefaultSerializationBinder.GetTypeFromTypeNameKey(TypeNameKey typeNameKey)
   at Newtonsoft.Json.Utilities.ThreadSafeStore`2.AddValue(TKey key)
   at Newtonsoft.Json.Utilities.ThreadSafeStore`2.Get(TKey key)
   at Newtonsoft.Json.Serialization.DefaultSerializationBinder.BindToType(String assemblyName, String typeName)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolveTypeName(JsonReader reader, Type& objectType, Js
onContract& contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, String
 qualifiedTypeName)
   --- End of inner exception stack trace ---
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolveTypeName(JsonReader reader, Type& objectType, Js
onContract& contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, String
 qualifiedTypeName)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadMetadataProperties(JsonReader reader, Type& objectT
ype, JsonContract& contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember,
 Object existingValue, Object& newValue, String& id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonCo
ntract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object exis
tingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType,
 JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Obje
ct existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean
 checkAdditionalContent)

看起来JSON.Net无法找到类System.Linq.Enumerable WhereSelectEnumerableIterator`2。我该怎么做才能解决这个问题?

0 个答案:

没有答案