Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Deserialize problem
5 replies. Latest Post by forci on October 29, 2008.
(0)
forci
Member
351 points
275 Posts
10-23-2008 8:33 AM |
I found some code on google to serialize/deserialize objects in silverlight, but it's not working.
In line:obj = (T)serializer.ReadObject(ms);
i get the next error: FileNotFoundException was unhandled by user codeCould not load file or assembly 'System.Runtime.Serialization, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.
I create 3 projects ASP.Net, silverlight application and silverlight library. Silverlight library has one object, that i want to serialize and transfer to server via WebService and then deserialize back to the original object.
I mada a test sample here.
Serialize code: public static byte[] Serialize<T>(T AObject) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(AObject.GetType()); MemoryStream ms = new MemoryStream(); serializer.WriteObject(ms, AObject); return ms.ToArray(); }
Deserialize code: public static T Deserialize<T>(byte[] AJSon) { T obj = Activator.CreateInstance<T>(); MemoryStream ms = new MemoryStream(AJSon); DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); obj = (T)serializer.ReadObject(ms); ms.Close(); return obj; }
Any ideas would be appreciated.
johnnystock
Contributor
2295 points
362 Posts
10-23-2008 12:13 PM |
I hate to pose the easy answer but did you add a reference to your silverlight application for System.Runtime.Serialization?
10-23-2008 5:08 PM |
Of course. If i wouldn't, the project wouldn't build. The error occur when running project.
10-27-2008 3:48 AM |
Does really no one has any idea?
Why i can not serialize deserialize class in WebService which is written in Silverlight library?
I would really appreciate if some one could get me solution.
Yi-Lun L...
All-Star
25052 points
2,747 Posts
10-27-2008 4:19 AM |
Hello, your WCF project is referencing the Silverlight class library. This is not supported. Silverlight and desktop .NET uses different CLR, and the assemblies can't be reused. Some source code can be reused with some tricks, such as link to the same source file, but assemblies can't. Anyway, why do you have to do so in this scenario? You create the classes in WCF project. When adding a service reference in your Silverlight project, the classes will automatically be created on the client side. When you update the service reference, the generated classes will also be regenerated.
10-29-2008 3:16 PM |
Thanks, that's all i wanted to know.