5

How can I use BinaryFormatter in Windows Phone 7.

I use this using System.Runtime.Serialization.Formatters.Binary in service1.svc.cs but I can't use this Reference in windows phone 7.

Is there any solution to this?

My Code For DeserializeObject

 public static T DeserializeObject<T>(byte[] xml)
    {
        BinaryFormatter xs = new BinaryFormatter();
        MemoryStream memoryStream = new MemoryStream(xml);
        return (T)xs.Deserialize(memoryStream);
    } 

BinaryFormatter gives error in windows phone 7. So how can I Deserialize. What changes that I have to do in this code?

Ajay
  • 6,418
  • 18
  • 79
  • 130
  • There is no solution with your service as written. You'll have to change the service to use a serialization format which is supported in Windows Phone 7. If your service is WCF why aren't you using the DataContractSerializer and letting WCF handle the serialization/deserialization? – Chris Dickson Sep 11 '12 at 22:00

3 Answers3

6

As @driis mentioned, you cannot use BinaryFormatter on Windows Phone. You can use the binary encoding in a WCF endpoint (i.e., an endpoint whose binding is a custom binding with the BinaryMessageEncodingBindingElement and the HttpTransportBindingElement), and that will be supported on WP7. You just cannot use the binary formatter there.

Update following comment: Looking at your code, it's not only that code that needs to be changed - you need to change the service code as well, to serialize an object in a format which is supported in Silverlight. You can use the DataContractSerializer, with a binary reader / writer, or you can use another library which is supported in both cases. For example, the code below should work in both desktop and SL versions:

public static T DeserializeObject<T>(byte[] xml) 
{ 
    using (MemoryStream memoryStream = new MemoryStream(xml))
    {
        using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(
            memoryStream, XmlDictionaryReaderQuotas.Max))
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(T));
            return (T)dcs.ReadObject(reader);
        }
    }
}

And on the server:

public static byte[] SerializeObject<T>(T obj)
{
    using (MemoryStream ms = new MemoryStream())
    {
        using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(ms))
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(T));
            dcs.WriteObject(writer, obj);
            writer.Flush();
            return ms.ToArray();
        }
    }
}
carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • This code give me error like this. Message = "Type 'System.Net.PathList' with data contract name 'PathList:http://schemas.datacontract.org/2004/07/System.Net' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types – Ajay Sep 12 '12 at 17:14
  • What type of object are you trying to serialize? – carlosfigueira Sep 12 '12 at 17:17
  • A cookie container is not serializable by the default serializers in SL, so you can either choose another serializer (IIRC it can be serialized by Newtonsoft's JSON.NET), or you can choose to use a Data Transfer Object to convert between the cookie container and a data type which can be serialized. – carlosfigueira Sep 14 '12 at 17:47
  • How can I Serialize and Deserialize using JSON.net. can you please give me code for this? – Ajay Sep 14 '12 at 17:52
  • Can U please check your code that u post as answer. It not work fine it gives error. or can please give me code for this using json.net. please I work on this error from last 5 days. yet I have not solved it – Ajay Sep 14 '12 at 19:48
2

BinaryFormatter is not supported in Windows Phone 7.

driis
  • 161,458
  • 45
  • 265
  • 341
  • You cannot. You need to switch to a serialization engine that is supported on both platforms. Perhaps protobuf-net, see the accepted answer here for some more info: http://stackoverflow.com/questions/5091712/binaryformatter-does-not-exist-in-cf-solutions – driis Sep 11 '12 at 19:44
0

There is no library support for binary serialization on WP 7.1. as driis mentioned.

You should rather use XmlObjectSerializer instead, or even one of it's subclasses that support serialization of most common API objects, like contacts data, or such.

If you checked System.Runtime.Serialization namespace assembly (ex. in Visual Studio's object browser) you'd found hierarchy of adequate classes.

And why do you care about serialization method? XML serialization is more portable, more uniform and easier to read by a human.

Krzysztof Jabłoński
  • 1,890
  • 1
  • 20
  • 29