我正在尝试反序列化 Silverlight 客户端中的一些 JSON 对象,这些对象通过 SignalR 发送到客户端。
这些对象已使用 TypeNameHandling = TypeNameHandling.All 设置进行序列化。
第一个问题是您不能将类库项目引用添加到 Silverlight 中,因此我使用了创建包含指向 POCO 类的链接的 Silverlight 类库的技巧。
但是在反序列化时出现错误,因为 JSON 字符串包含完整类型,包括程序集(这是不同的)。如果我操作 JSON 字符串并替换程序集名称,仍然会得到相同的错误。
这些是 POCO:
public abstract class MessageBase
{
public Exception Exception { get; set; }
}
public class AccessRCImported : MessageBase
{
public DateTime Cob { get; set; }
}
这是 JSON 字符串:
{"$type":"CS.RAR.ICE.Common.Messages.AccessRCImported, CS.RAR.ICE","Cob":"2012-08-01T00:00:00","Exception":null}
已被连载
var output = JsonConvert.SerializeObject(message,
new JsonSerializerSettings
{TypeNameHandling = TypeNameHandling.All});
// Replace the assembly name
var result = output.Replace(", CS.RAR.ICE.Common", ", CS.RAR.ICE");
var ctx = ConnectionManager.GetHubContext<ServerHub>();
ctx.Clients.Notify(result);
然后在 Silverlight 客户端上,我正在做
var message = JsonConvert.DeserializeObject< MessageBase>(onData, _serializerSettings);
我得到以下错误。Silverlight 应用程序和 Silverlight 客户端库都以 Silverlight 5 为目标。
有任何想法吗?
谢谢。
{Newtonsoft.Json.JsonSerializationException: Error resolving type specified in JSON 'CS.RAR.ICE.Common.Messages.AccessRCImported, CS.RAR.ICE'. Path '$type', line 1, position 66. ---> System.IO.FileLoadException: Could not load file or assembly 'CS.RAR.ICE, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The requested assembly version conflicts with what is already bound in the app domain or specified in the manifest. (Exception from HRESULT: 0x80131053)
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
at Newtonsoft.Json.Serialization.DefaultSerializationBinder.GetTypeFromTypeNameKey(TypeNameKey typeNameKey)
at Newtonsoft.Json.Utilities.ThreadSafeStore
2.Newtonsoft.Json.Utilities.ThreadSafeStore 的AddValue(TKey key)2.Get(TKey key)
at Newtonsoft.Json.Serialization.DefaultSerializationBinder.BindToType(String assemblyName, String typeName)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadSpecialProperties(JsonReader reader, Type& objectType, JsonContract& contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue, Object& newValue, String& id)
--- End of inner exception stack trace ---
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadSpecialProperties(JsonReader reader, Type& objectType, JsonContract& contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue, Object& newValue, 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.JsonSerializer.Deserialize(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 CS.RAR.ICE.ClientHub.Handle(String onData)}