Exception: SerializationException

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brent Starkes

    #1

    Exception: SerializationException

    Hi,

    I'm having a deserialization problem with a dll plug-in I'm writing, it
    serializes just fine but I'm getting an exception on the deserialization
    process.

    A first chance exception of type
    'System.Runtime .Serialization. SerializationEx ception' occurred in
    mscorlib.dll
    DeserializeTopH at: Unable to find assembly 'Tools, Version=0.2005. 22.0,
    Culture=neutral , PublicKeyToken= null'.

    Any ideas would be greatly appreciated.

    Brent

    Here is my class that I want to serialize:

    [Serializable()]
    public class TopHat : ISerializable
    {
    public string Description;
    public decimal BaseTH;
    public decimal Crown;
    public decimal Flange;
    public decimal Webdepth;
    public decimal Parameter;
    public bool Finished;

    public TopHat()
    {
    Description = String.Empty;
    BaseTH = 0.0M;
    Crown = 0.0M;
    Flange = 0.0M;
    Webdepth = 0.0M;
    Parameter = 0.0M;
    Finished = false;
    }


    //Deserialization constructor
    public TopHat(Serializ ationInfo info, StreamingContex t ctxt)
    {
    Description = (String)info.Ge tValue("Descrip tion",
    typeof(string)) ;
    BaseTH = (decimal)info.G etValue("BaseTH ", typeof(decimal) );
    Crown = (decimal)info.G etValue("Crown" , typeof(decimal) );
    Flange = (decimal)info.G etValue("Flange ", typeof(decimal) );
    Webdepth = (decimal)info.G etValue("Webdep th", typeof(decimal) );
    Parameter = (decimal)info.G etValue("Parame ter",
    typeof(decimal) );
    Finished = (bool)info.GetV alue("Finished" , typeof(bool));
    }

    //Serialization function
    public void GetObjectData(S erializationInf o info, StreamingContex t ctxt)
    {
    info.AddValue(" Description", Description);
    info.AddValue(" BaseTH", BaseTH);
    info.AddValue(" Crown", Crown);
    info.AddValue(" Flange", Flange);
    info.AddValue(" Webdepth", Webdepth);
    info.AddValue(" Parameter", Parameter);
    info.AddValue(" Finished", Finished);
    }

    public override string ToString()
    {
    return Description;
    }
    }

    Here is where I serailize, first I dump to a temp file and then read that
    into a byte[] array so I can store it on a OLEObject in my access database:

    string filelocation = Application.Use rAppDataPath + "\\blah.tmp ";
    Stream a = File.Open(filel ocation, FileMode.Create );
    BinaryFormatter bformatter = new BinaryFormatter ();
    bformatter.Seri alize(a, this._tp.Tophat );
    System.Byte[] s_shape = new byte[a.Length];
    a.Position = 0;
    for (long i = 0; i < a.Length; i++)
    s_shape[i] = (byte)a.ReadByt e();
    a.Close();

    this.daShapes.F ill(this.dsShap es, "shapes");
    System.Data.Dat aRow newRow = this.dsShapes.T ables["shapes"].NewRow();
    newRow["typeid"] = Shapes.TopHat;
    newRow["shape"] = s_shape;


    this works just fine.

    This is what I do when I deserial

    TopHat tophat = new TopHat();
    string filelocation = Application.Use rAppDataPath +
    "\\blah.tmp ";
    Stream stream = File.Open(filel ocation, FileMode.Create );
    try
    {
    stream.Write(s_ shape, 0, s_shape.Length) ;
    stream.Position = 0;

    BinaryFormatter bformatter = new BinaryFormatter ();
    object o = bformatter.Dese rialize(stream) ;
    tophat = (TopHat)o;
    }
    catch (Exception ex)
    {
    Debug.WriteLine ("DeserializeTo pHat: " + ex.Message);
    }
    finally
    {
    stream.Close();
    }


  • Mark R. Dawson

    #2
    RE: Exception: SerializationEx ception

    Hi Brent,
    the problem is that the code cannot deserialize the types because it
    cannot find the assembly that contains the type information which will allow
    it to reassemble to objects. You can do two things:

    1. Register the assembly in the GAC
    2. Make sure the assembly is in a place where the executing code can find it
    such as in the executing directory.

    Hope that helps
    Mark Dawson



    "Brent Starkes" wrote:
    [color=blue]
    > Hi,
    >
    > I'm having a deserialization problem with a dll plug-in I'm writing, it
    > serializes just fine but I'm getting an exception on the deserialization
    > process.
    >
    > A first chance exception of type
    > 'System.Runtime .Serialization. SerializationEx ception' occurred in
    > mscorlib.dll
    > DeserializeTopH at: Unable to find assembly 'Tools, Version=0.2005. 22.0,
    > Culture=neutral , PublicKeyToken= null'.
    >
    > Any ideas would be greatly appreciated.
    >
    > Brent
    >
    > Here is my class that I want to serialize:
    >
    > [Serializable()]
    > public class TopHat : ISerializable
    > {
    > public string Description;
    > public decimal BaseTH;
    > public decimal Crown;
    > public decimal Flange;
    > public decimal Webdepth;
    > public decimal Parameter;
    > public bool Finished;
    >
    > public TopHat()
    > {
    > Description = String.Empty;
    > BaseTH = 0.0M;
    > Crown = 0.0M;
    > Flange = 0.0M;
    > Webdepth = 0.0M;
    > Parameter = 0.0M;
    > Finished = false;
    > }
    >
    >
    > //Deserialization constructor
    > public TopHat(Serializ ationInfo info, StreamingContex t ctxt)
    > {
    > Description = (String)info.Ge tValue("Descrip tion",
    > typeof(string)) ;
    > BaseTH = (decimal)info.G etValue("BaseTH ", typeof(decimal) );
    > Crown = (decimal)info.G etValue("Crown" , typeof(decimal) );
    > Flange = (decimal)info.G etValue("Flange ", typeof(decimal) );
    > Webdepth = (decimal)info.G etValue("Webdep th", typeof(decimal) );
    > Parameter = (decimal)info.G etValue("Parame ter",
    > typeof(decimal) );
    > Finished = (bool)info.GetV alue("Finished" , typeof(bool));
    > }
    >
    > //Serialization function
    > public void GetObjectData(S erializationInf o info, StreamingContex t ctxt)
    > {
    > info.AddValue(" Description", Description);
    > info.AddValue(" BaseTH", BaseTH);
    > info.AddValue(" Crown", Crown);
    > info.AddValue(" Flange", Flange);
    > info.AddValue(" Webdepth", Webdepth);
    > info.AddValue(" Parameter", Parameter);
    > info.AddValue(" Finished", Finished);
    > }
    >
    > public override string ToString()
    > {
    > return Description;
    > }
    > }
    >
    > Here is where I serailize, first I dump to a temp file and then read that
    > into a byte[] array so I can store it on a OLEObject in my access database:
    >
    > string filelocation = Application.Use rAppDataPath + "\\blah.tmp ";
    > Stream a = File.Open(filel ocation, FileMode.Create );
    > BinaryFormatter bformatter = new BinaryFormatter ();
    > bformatter.Seri alize(a, this._tp.Tophat );
    > System.Byte[] s_shape = new byte[a.Length];
    > a.Position = 0;
    > for (long i = 0; i < a.Length; i++)
    > s_shape[i] = (byte)a.ReadByt e();
    > a.Close();
    >
    > this.daShapes.F ill(this.dsShap es, "shapes");
    > System.Data.Dat aRow newRow = this.dsShapes.T ables["shapes"].NewRow();
    > newRow["typeid"] = Shapes.TopHat;
    > newRow["shape"] = s_shape;
    >
    >
    > this works just fine.
    >
    > This is what I do when I deserial
    >
    > TopHat tophat = new TopHat();
    > string filelocation = Application.Use rAppDataPath +
    > "\\blah.tmp ";
    > Stream stream = File.Open(filel ocation, FileMode.Create );
    > try
    > {
    > stream.Write(s_ shape, 0, s_shape.Length) ;
    > stream.Position = 0;
    >
    > BinaryFormatter bformatter = new BinaryFormatter ();
    > object o = bformatter.Dese rialize(stream) ;
    > tophat = (TopHat)o;
    > }
    > catch (Exception ex)
    > {
    > Debug.WriteLine ("DeserializeTo pHat: " + ex.Message);
    > }
    > finally
    > {
    > stream.Close();
    > }
    >
    >
    >[/color]

    Comment

    Working...