Deserializing JSON objects to POCO objects in Silverlight

1

I'm trying to deserialize some JSON objects in a Silverlight client, which are sent over to the client via SignalR.

These objects have been serialized using the TypeNameHandling = TypeNameHandling.All settings.

The first problem is that you can't add a Class library project reference to the Silverlight one, so I have used the trick of creating a Silverlight Class library that contains a link to the POCO classes.

But then I get an error when deserializing, because the JSON string contains the full type including assembly (which is different). If I manipulate the JSON string and replace the assembly name, still get the same error.

These are the POCOs:

public abstract class MessageBase
{
    public Exception Exception { get; set; }
}    

public class AccessRCImported : MessageBase
{
    public DateTime Cob { get; set; }
}

This is the JSON string:

{"$type":"CS.RAR.ICE.Common.Messages.AccessRCImported, CS.RAR.ICE","Cob":"2012-08-01T00:00:00","Exception":null}

Which has been serialized by

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);

Then on the Silverlight client, I'm doing

var message = JsonConvert.DeserializeObject< MessageBase>(onData, _serializerSettings);

And I get below error. Both the Silverlight app and the Silverlight Client library are targetting Silverlight 5.

Any ideas?

Thanks.

{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.ThreadSafeStore2.AddValue(TKey key) at Newtonsoft.Json.Utilities.ThreadSafeStore2.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)}

silverlight
json.net
signalr
asked on Stack Overflow Aug 1, 2012 by Alberto

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0