From: Joe Cool on
I have this class in a project:

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

namespace myProject
{
[Serializable]
public class Attachment
{

#region class variables

ArrayList _fields = null;
string _application;
int _docid;

#endregion


#region contructors

public Attachment()
{
_fields = new ArrayList();
_application = string.Empty;
_docid = 0;
}

#endregion


#region properties

public string Application
{
get { return _application; }
set { _application = value; }
}

public ArrayList Fields
{
get { return _fields; }
set { _fields = value; }
}

public int DocID
{
get { return _docid; }
set { _docid = value; }
}

#endregion


#region public methods

public byte[] Serialize()
{
MemoryStream memoryStream = null;
BinaryFormatter formatter = null;

try
{
memoryStream = new MemoryStream();

formatter = new BinaryFormatter();

formatter.Serialize(memoryStream, this);

return memoryStream.ToArray();
}
catch (Exception ex)
{
return null;
}
finally
{
memoryStream.Close();
}
}

public void Deserialize(byte[] attachmentBytes)
{
BinaryFormatter formatter = null;
MemoryStream memoryStream = null;
Attachment tempAttachment = null;
object tempObject = null;

try
{
formatter = new BinaryFormatter();

memoryStream = new MemoryStream(attachmentBytes);

formatter.Binder = new GenericBinder();

memoryStream.Position = 0;

tempObject = formatter.Deserialize(memoryStream);

tempAttachment = (Attachment)tempObject;

this.Application = tempAttachment.Application;
this.Fields = tempAttachment.Fields;
this.DocID = tempAttachment.DocID;
}
catch(Exception ex)
{
return;
}
}

#endregion

}
}

When I try to invoke the Deserialize on a byte array that I just
serialized, it throws this exception:

Binary stream '0' does not contain a valid BinaryHeader. Possible
causes are invalid stream or object version change between
serialization and deserialization.

I have googled t his exception and cannot find any solution that fixes
my problem. Any help would be appreciated.
From: Peter Duniho on
On Wed, 23 Sep 2009 12:29:39 -0700, Joe Cool <joecool1969(a)live.com> wrote:

> [...]
> When I try to invoke the Deserialize on a byte array that I just
> serialized, it throws this exception:
>
> Binary stream '0' does not contain a valid BinaryHeader. Possible
> causes are invalid stream or object version change between
> serialization and deserialization.
>
> I have googled t his exception and cannot find any solution that fixes
> my problem. Any help would be appreciated.

Most likely, you have made a mistake either with the bytes being
deserialized, or the GenericBinder() class is doing something odd.

Since you didn't post the specifics regarding either of those aspects,
never mind a concise-but-complete code example that reliably reproduces
the problem, there's no way for anyone to say for sure what's wrong.

Pete
From: Joe Cool on
On Sep 23, 3:45 pm, "Peter Duniho" <no.peted.s...(a)no.nwlink.spam.com>
wrote:
> On Wed, 23 Sep 2009 12:29:39 -0700, Joe Cool <joecool1...(a)live.com> wrote:
> > [...]
> > When I try to invoke the Deserialize on a byte array that I just
> > serialized, it throws this exception:
>
> > Binary stream '0' does not contain a valid BinaryHeader. Possible
> > causes are invalid stream or object version change between
> > serialization and deserialization.
>
> > I have googled t his exception and cannot find any solution that fixes
> > my problem. Any  help would be appreciated.
>
> Most likely, you have made a mistake either with the bytes being  
> deserialized, or the GenericBinder() class is doing something odd.
>
> Since you didn't post the specifics regarding either of those aspects,  
> never mind a concise-but-complete code example that reliably reproduces  
> the problem, there's no way for anyone to say for sure what's wrong.
>
> Pete

WIll this help? Here is the GenericBinder class:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Text;

namespace myProject
{
public class GenericBinder : SerializationBinder
{
/// <summary>
/// Resolve type
/// </summary>
/// <param name="assemblyName">eg. App_Code.y4xkvcpq,
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null</param>
/// <param name="typeName">eg. String</param>
/// <returns>Type for the deserializer to use</returns>
public override Type BindToType(string assemblyName, string
typeName)
{
// We're going to ignore the assembly name, and assume
it's in the same assembly
// that <T> is defined (it's either T or a field/return
type within T anyway)
string[] typeInfo = typeName.Split('.');
bool isSystem = (typeInfo[0].ToString() == "System");
string className = typeInfo[typeInfo.Length - 1];

// noop is the default, returns what was passed in
Type toReturn = Type.GetType(string.Format("{0}, {1}",
typeName, assemblyName));

if (!isSystem && (toReturn == null))
{ // don't bother if system, or if the GetType worked
already (must be OK, surely?)
System.Reflection.Assembly a =
System.Reflection.Assembly.GetAssembly(typeof(Attachment));
if (a == null)
{
throw new ArgumentException("Assembly for type '"
+ typeof(Attachment).Name.ToString() + "' could not be loaded.");
}
else
{
string assembly = a.FullName.Split(',')[0]; //
FullName example: "App_Code.y4xkvcpq, Version=0.0.0.0,
Culture=neutral, PublicKeyToken=null"
Type newtype = a.GetType(assembly + "." +
className);
if (newtype == null)
{
throw new ArgumentException("Type '" +
typeName + "' could not be loaded from assembly '" + assembly + "'.");
}
else
{
toReturn = newtype;
}
}
}
return toReturn;
}
}
}
From: Joe Cool on
On Sep 23, 3:45 pm, "Peter Duniho" <no.peted.s...(a)no.nwlink.spam.com>
wrote:
> On Wed, 23 Sep 2009 12:29:39 -0700, Joe Cool <joecool1...(a)live.com> wrote:
> > [...]
> > When I try to invoke the Deserialize on a byte array that I just
> > serialized, it throws this exception:
>
> > Binary stream '0' does not contain a valid BinaryHeader. Possible
> > causes are invalid stream or object version change between
> > serialization and deserialization.
>
> > I have googled t his exception and cannot find any solution that fixes
> > my problem. Any  help would be appreciated.
>
> Most likely, you have made a mistake either with the bytes being  
> deserialized, or the GenericBinder() class is doing something odd.
>
> Since you didn't post the specifics regarding either of those aspects,  
> never mind a concise-but-complete code example that reliably reproduces  
> the problem, there's no way for anyone to say for sure what's wrong.
>
> Pete

Oh, and the bytes being deserialized was a byte array that had been
generated by this same class's Serialize method, stored in a database
as a byte array, and later retrieved for Deserialization.
From: Peter Duniho on
On Wed, 23 Sep 2009 12:59:15 -0700, Joe Cool <joecool1969(a)live.com> wrote:

> WIll this help? Here is the GenericBinder class: [...]

Here are some links you may find helpful:
http://www.yoda.arachsys.com/csharp/complete.html
http://www.yoda.arachsys.com/csharp/incomplete.html
http://sscce.org/ (some Java-centric stuff, but mostly applicable to any
programming questions)