How do I deserialize the bytes I've sent?

384 views Asked by At

I have connection between Main server and slave servers. I want in the main to Serialize message and in the slave to Deserializing it as an object. So I tried to do it and I got an exception :

The input stream is not a valid binary format. The starting contents (in bytes) are: 03-00-01-3F-C3-F5-E4-41-00-C0-8D-C3-14-EE-4A-C3-00 ...

I marked in the code where it falls in the DeSerialize.

I want my message to look like this : ID message(like 1\2\3) + OBJ :

 -----------------------------
|  ID  | OBJECT              |
 -----------------------------

My code :

    public override byte[] Serialize()
        {
            byte[] byteToSend = new byte[1024];
            MemoryStream stream = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
            formatter.Serialize(stream, this);
            byteToSend[0] = 3;
            Array.Copy(stream.ToArray(), 0, byteToSend, 3, stream.ToArray().Length);
            return byteToSend;
        }

        internal sealed class VersionConfigToNamespaceAssemblyObjectBinder : SerializationBinder
        {
            public override Type BindToType(string assemblyName, string typeName)
            {
                Type typeToDeserialize = null;
                try
                {
                    string ToAssemblyName = assemblyName.Split(',')[0];
                    Assembly[] Assemblies = AppDomain.CurrentDomain.GetAssemblies();
                    foreach (Assembly ass in Assemblies)
                    {
                        if (ass.FullName.Split(',')[0] == ToAssemblyName)
                        {
                            typeToDeserialize = ass.GetType(typeName);
                            break;
                        }
                    }
                }
                catch (System.Exception exception)
                {
                    throw exception;
                }
                return typeToDeserialize;
            }
        }

        public override BaseMassage DeSerialize(byte[] data)
        {
            MemoryStream stream = new MemoryStream(data);
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
            formatter.Binder = new VersionConfigToNamespaceAssemblyObjectBinder();
            RadarMassage obj = (RadarMassage)formatter.Deserialize(stream); // ---- Here it falls
            return obj;
}

I found this example after a lot of searching. Thank's in advice. :)

1

There are 1 answers

3
nkj On BEST ANSWER

Try this code

public override BaseMassage DeSerialize(byte[] data)
{
        MemoryStream stream = new MemoryStream(data, 3, data.Length - 3);
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
        formatter.Binder = new VersionConfigToNamespaceAssemblyObjectBinder();
        RadarMassage obj = (RadarMassage)formatter.Deserialize(stream);
        return obj;
}