Binary serialization C#: deserialize method give me Exceptions

678 Views Asked by At

I have a problem with Serialization/Deserialization in Wpf applications that comunicate between socket. In details: In both of apps i have a class that implement ISerializable:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace GraficaClient
{
    [Serializable()]    //Set this attribute to all the classes that want to serialize
    class SerializerObject : ISerializable
    {
        public String type;
        public String txt;
        /*public String rft;
        public Byte[] audio;
        public Byte[] img;
        */

        public SerializerObject()
        {
        }

        //Deserialization constructor.
        public SerializerObject(SerializationInfo info, StreamingContext ctxt)
        {
            //Get the values from info and assign them to the appropriate properties
            type = (String)info.GetValue("type", typeof(String));
            txt= (String)info.GetValue("txt", typeof(String));
            /*   rft = (String)info.GetValue("rft", typeof(String));
            audio = (Byte[])info.GetValue("audio", typeof(Byte[]));
            img = (Byte[])info.GetValue("img", typeof(Byte[]));*/
        }

        //Serialization function.
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("type", type);
            info.AddValue("txt", txt);
            /*info.AddValue("rft", rft);
            info.AddValue("audio",audio);
            info.AddValue("img", img);*/
        }
    }
}

In one of my application i put inside the informations(in this case only tow strings) and serialize it in a stream

TcpClient c = new TcpClient();
SerializerObject o = new SerializerObject();
o.type="t";
o.text="hello";

c.Connect(ip,port);
NetworkStream stream = c.GetStream();
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, o);

on the otherside

TcpClient client = server.AcceptTcpClient(); 

NetworkStream stream = client.GetStream();
BinaryFormatter b = new BinaryFormatter();
SerializerObject o= (SerializerObject)b.Deserialize(stream);

This instruction give me an Exception A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll

and if i print e.message="impossible to find l'assembly 'ProgettoServerV2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'."

e.stacktrace= in System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()\r\n in System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)\r\n in System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)\r\n in System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)\r\n in System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)\r\n in System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()\r\n in System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)\r\n in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)\r\n in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)\r\n in GraficaClient.GestoreClipboard.gestioneClipboard() in c:\Users\pietro\Desktop\GraficaClient\GraficaClient\GestoreClipboard.cs:riga 91

currently i'm using VisualStudio 2013. How can i solve it.

1

There are 1 best solutions below

0
On

Make sure the program can load the missing assembly. What is so hard to understand at the error message? Met me quote you the ONLY relevant piece of inforamtion in your page long post:

e.message="impossible to find l'assembly 'ProgettoServerV2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'."

Make sure this assembly is available and deserialization will work.