Skip to main content

Microsoft Silverlight

Answered Question Deserialize problemRSS Feed

(0)

forci
forci

Member

Member

351 points

275 Posts

Deserialize problem

 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 code
Could 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.

Make love not war.

johnnystock
johnnystock

Contributor

Contributor

2295 points

362 Posts

Silverlight MVP

Re: Deserialize problem

I hate to pose the easy answer but did you add a reference to your silverlight application for System.Runtime.Serialization?

John Stockton
Microsoft Silverlight MVP and RIA Developer at Ascentium
Co-Author of Silverlight 2 in Action

forci
forci

Member

Member

351 points

275 Posts

Re: Re: Deserialize problem

Of course. If i wouldn't, the project wouldn't build. The error occur when running project.

Make love not war.

forci
forci

Member

Member

351 points

275 Posts

Re: Re: Re: Deserialize problem

 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.

Make love not war.

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

Re: Deserialize problem

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.

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

forci
forci

Member

Member

351 points

275 Posts

Re: Re: Deserialize problem

 Thanks, that's all i wanted to know.

Make love not war.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities