Binary stream does not contain a valid BinaryHeader

Discussion in 'ASP .Net' started by BH, Aug 29, 2003.

  1. BH

    BH Guest

    I'm trying a simple object serialization and deserialization, and keep
    getting this error:

    System.Runtime.Serialization.SerializationException: Binary stream does not
    contain a valid BinaryHeader, 0 possible causes, invalid stream or object
    version change between serialization and deserialization.

    Here's my code. it does nothing but to serialize a DataTable object into a
    byte array, and then read the byte array back for deserialization. I
    verified that the byte array length did not change. the error occurs on the
    object newObj = formatter.Deserialize(ms2); line.

    public class Test {
    Public Test {

    DataTable dt = this.CreateDataSource();
    System.IO.Stream ms= new System.IO.MemoryStream();
    IFormatter formatter = new BinaryFormatter();
    formatter.Serialize(ms, dt);

    int numBytesToRead = (int) ms.Length;
    byte[] bytes = new byte[numBytesToRead];
    int numBytesRead = 0;
    int n = ms.Read(bytes, numBytesRead, numBytesToRead);
    ms.Position = 0;
    ms.Close();

    System.IO.Stream ms2= new System.IO.MemoryStream();
    int numBytesToWrite = bytes.Length;
    ms2.SetLength(numBytesToWrite);
    int numBytesWritten = 0;
    ms2.Write(bytes, numBytesWritten, numBytesToWrite);

    ms2.Position = 0;
    object newObj = formatter.Deserialize(ms2);

    ms2.Close();
    }


    private DataTable CreateDataSource() {
    DataTable dt = new DataTable();
    DataRow dr;

    dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
    dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
    dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

    for (int i = 0; i < 10; i++) {
    dr = dt.NewRow();

    dr[0] = i;
    dr[1] = "Item " + i.ToString();
    dr[2] = 1.23 * (i+1);

    dt.Rows.Add(dr);
    }
    return dt;
    }
    }
    BH, Aug 29, 2003
    #1
    1. Advertising

  2. BH

    Jon Skeet Guest

    BH <> wrote:
    > I'm trying a simple object serialization and deserialization, and keep
    > getting this error:
    >
    > System.Runtime.Serialization.SerializationException: Binary stream does not
    > contain a valid BinaryHeader, 0 possible causes, invalid stream or object
    > version change between serialization and deserialization.
    >
    > Here's my code.


    That's not your code. When you're going to post code, *please* post the
    actual code - the code you posted doesn't compile. However, when
    (adding using statements, etc) I got your code to compile, the problem
    is pretty simple - and it's nothing to do with serialization, really.

    > public class Test {
    > Public Test {
    >
    > DataTable dt = this.CreateDataSource();
    > System.IO.Stream ms= new System.IO.MemoryStream();
    > IFormatter formatter = new BinaryFormatter();
    > formatter.Serialize(ms, dt);
    >
    > int numBytesToRead = (int) ms.Length;
    > byte[] bytes = new byte[numBytesToRead];
    > int numBytesRead = 0;
    > int n = ms.Read(bytes, numBytesRead, numBytesToRead);
    > ms.Position = 0;
    > ms.Close();


    All the mistakes are in this section.

    Firstly, you're trying to read *before* rewinding the stream - in other
    words, you write to the stream, but then try to read *from the end*.

    Secondly, when you read you're trying to read, you're trying to read at
    the end of the byte array rather than the start.

    Thirdly, you're assuming that everything will be read in one chunk
    (which it probably will with a MemoryStream, but don't make that
    assumption for streams in general). See
    http://www.yoda.arachsys.com/csharp/readbinary.html for more details.

    Fourthly, you're not flushing the stream after writing to it. Close()
    will do this for you, and in a MemoryStream it's probably not necessary
    anyway, but if you're going to do anything other than write then close,
    I'd recommend calling flush anyway.

    Now, taking the above points into consideration (except number 3 for
    the moment, because it wasn't harming your code in reality, would be a
    minor pain to demonstrate, is unnecessary for reasons explained in a
    moment, and probably wouldn't be what you'd use in real serialization)
    we end up with:

    DataTable dt = CreateDataSource();
    System.IO.Stream ms= new System.IO.MemoryStream();
    IFormatter formatter = new BinaryFormatter();
    formatter.Serialize(ms, dt);
    ms.Flush();
    ms.Position = 0;

    int numBytesToRead = (int) ms.Length;
    byte[] bytes = new byte[numBytesToRead];
    ms.Read(bytes, 0, numBytesToRead);
    ms.Close();

    which works.

    However, simpler is:
    DataTable dt = CreateDataSource();
    System.IO.Stream ms= new System.IO.MemoryStream();
    IFormatter formatter = new BinaryFormatter();
    formatter.Serialize(ms, dt);
    ms.Flush();
    byte[] bytes = ms.ToArray();
    ms.Close();

    Of course, simpler than that is just to rewind the memory stream and
    use the same stream for deserializing as we just serialized into:

    DataTable dt = CreateDataSource();
    System.IO.Stream ms= new System.IO.MemoryStream();
    IFormatter formatter = new BinaryFormatter();
    formatter.Serialize(ms, dt);
    ms.Flush();
    ms.Position=0;
    object newObj = formatter.Deserialize(ms);
    ms.Close();

    but maybe that doesn't show what you wanted to show.

    Anyway, hope this helped.

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet/
    If replying to the group, please do not mail me too
    Jon Skeet, Aug 31, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ashish Kanoongo
    Replies:
    1
    Views:
    611
    Vjekoslav Babic
    Feb 3, 2004
  2. =?Utf-8?B?ZXRyb3BpYw==?=

    'System.Data.DataRowView' does not contain a property ......

    =?Utf-8?B?ZXRyb3BpYw==?=, May 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,646
    =?Utf-8?B?ZXRyb3BpYw==?=
    May 10, 2004
  3. Niko Korhonen

    Can std::string contain binary data

    Niko Korhonen, Jan 10, 2004, in forum: C++
    Replies:
    1
    Views:
    6,205
    Bob Hairgrove
    Jan 10, 2004
  4. Roger Pack
    Replies:
    3
    Views:
    154
    Roger Pack
    Sep 28, 2010
  5. Jason Carlton
    Replies:
    11
    Views:
    245
    Dr J R Stockton
    Dec 8, 2009
Loading...

Share This Page