Deserialization Error--End of Stream

G

Guest

Hello,

I am attempting to serialize an object for storage in a DB field and them deserialize it later on. I have managed to Serialize it using

private string SerializeObject(object ObjectToSerialize)
{
MemoryStream ms = new MemoryStream();
string theSerializedObject;

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
try
{
formatter.Serialize(ms, ObjectToSerialize);
byte[] thisByteArray = ms.ToArray();
theSerializedObject = Convert.ToBase64String(thisByteArray);
}
catch (System.Runtime.Serialization.SerializationException e)
{
Logs.WriteToLog("Error Serializing " + ObjectToSerialize.GetType().ToString() + " Object: " + e.Message);
throw;
}
finally
{
ms.Close();
}

return theSerializedObject;
}
I store the string returned by the above method in the DB. But I am having to trouble Deserializing it. I have the following method:

private object DeserializeObject(string StringToDeserialize)
{
object thisDeserializedObject = null;
byte[] thisByteArray = Convert.FromBase64String(StringToDeserialize);
MemoryStream ms = new MemoryStream(thisByteArray);
try
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

thisDeserializedObject = formatter.Deserialize(ms);
}
catch (System.Runtime.Serialization.SerializationException e)
{
Logs.WriteToLog("Error Deserializing " + StringToDeserialize + " String: " + e.Message);
throw;
}
finally
{
ms.Close();
}

return thisDeserializedObject;
}

The deserialized method raises an error saying "End of Stream encountered before parsing was completed." when it reaches

thisDeserializedObject = formatter.Deserialize(ms);

What might be causing this? I could provide some sample data if it would help. Thanks for your help!
 
S

Steven Cheng[MSFT]

Hi ,

From your description, you're using the
System.Runtime.Serialization.Formatters.Binary.BinaryFomatter to serialize
a class object and store the output binary data into db as base64string.
However, when you retireve the data back from db and try to deserialize on
it, you get error , yes?

As for this problem, I think we can first try the following steps:
1.Try serialize the object via FileStream and store on the local file
system and deserilize from the file to see whether this work. Thus, we can
confirm whether the problem is concerned with the database or not.

2. Since you convert the serialize output into base64 string and stored in
db. What's the columns' datatype in db? Text? or varchar? or ... I
suggest that you check the byte array or base64 sttring's length before
stored into db and when retrieved back from db to see whether the length
are identical or the content has been modified.

If you have any other findings, please also feel free to post here.

Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Steven,

Thank you for the suggestion. I did a test

Issue myIssue = new Issue();
string IssueString = new StringFromObject(myIssue);
Issue myDeserializedIssue = new StringToObject(IssueString);

and this worked! So the issue is apparently with the data storage method. I am using ntext to store the data. Both the ntext and the Image types don't work for storing the serialized string. Do you have any suggestions? Thanks.
 
M

Marcin

Solel said:
Based on your suggestion I looked into this further and was able to determine the problem. I was truncating the data by setting a limit to the stored procedure parameter:

SqlParameter parameterData = new SqlParameter("@Data", SqlDbType.NText, 16);

Once I removed the 16 it worked! Thanks for your help.

I would like to ask you how to serialize SqlParameter. When I try it,
I've error:

"The type System.Data.SqlClient.SqlParameter in Assembly System.Data,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 is
not marked as serializable."

What I do wrong ?
How do I have to implement this?

Thanks,
Marcin
 
G

Guest

Marcin,

I'm not sure why you are looking to serialize the SqlParameter itself. The
error you are getting is caused by the SqlParameter class not being
designated as serializable. This class is part of the .NET Framework so you
are not able to change it to be serializable (only Microsoft could do so).
However that shouldn't matter because if you are attempting to store the
serialized data in the stored procedure parameter you just have to serialize
the data into a string or binary and not the SqlParameter. For example let's
say you have a class

[Serializable()]
public class MyData
{
private int _myInt = 0;
public int myInt{ get{ return _myInt;} set{ _myInt = value;}}
}

that you would like to store in the database. Notice the [Serializable()]
attribute above the name. That tells the .NET compiler to compile the class
as serializable. You would then serialize an instance of this class
containing the data you are looking to store in the database. You would set
the SqlParameter to that serialized data. I use the following utility
methods I put together to serialize/deserialize the object to and from a
string.

private string StringFromObject(object ObjectToSerialize)
{
MemoryStream ms = new MemoryStream();
string theSerializedObject;

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
try
{
formatter.Serialize(ms, ObjectToSerialize);
byte[] thisByteArray = ms.ToArray();
theSerializedObject = Convert.ToBase64String(thisByteArray);
}
catch (System.Runtime.Serialization.SerializationException e)
{
Logs.WriteToLog("Error Serializing " +
ObjectToSerialize.GetType().ToString() + " Object: " + e.Message);
throw;
}
finally
{
ms.Close();
}

return theSerializedObject;
}

private object StringToObject(string StringToDeserialize)
{
object thisDeserializedObject = null;
byte[] thisByteArray = Convert.FromBase64String(StringToDeserialize);
MemoryStream ms = new MemoryStream(thisByteArray);
try
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

thisDeserializedObject = formatter.Deserialize(ms);
}
catch (System.Runtime.Serialization.SerializationException e)
{
Logs.WriteToLog("Error Deserializing " + StringToDeserialize + " String:
" + e.Message);
throw;
}
finally
{
ms.Close();
}

return thisDeserializedObject;
}

Once I've serialized the object to a string I simply set the SqlParameter to
the string. Does this answer your question?
 
M

Marcin

I'm not sure why you are looking to serialize the SqlParameter itself. The
error you are getting is caused by the SqlParameter class not being
designated as serializable. This class is part of the .NET Framework so you
are not able to change it to be serializable (only Microsoft could do so).
However that shouldn't matter because if you are attempting to store the
serialized data in the stored procedure parameter you just have to serialize
the data into a string or binary and not the SqlParameter. For example let's
say you have a class

[Serializable()]
public class MyData
{
private int _myInt = 0;
public int myInt{ get{ return _myInt;} set{ _myInt = value;}}
}

that you would like to store in the database. Notice the [Serializable()]
attribute above the name. That tells the .NET compiler to compile the class
as serializable. You would then serialize an instance of this class
containing the data you are looking to store in the database. You would set
the SqlParameter to that serialized data. I use the following utility
methods I put together to serialize/deserialize the object to and from a
string.

private string StringFromObject(object ObjectToSerialize)
{
MemoryStream ms = new MemoryStream();
string theSerializedObject;

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
try
{
formatter.Serialize(ms, ObjectToSerialize);
byte[] thisByteArray = ms.ToArray();
theSerializedObject = Convert.ToBase64String(thisByteArray);
}
catch (System.Runtime.Serialization.SerializationException e)
{
Logs.WriteToLog("Error Serializing " +
ObjectToSerialize.GetType().ToString() + " Object: " + e.Message);
throw;
}
finally
{
ms.Close();
}

return theSerializedObject;
}

private object StringToObject(string StringToDeserialize)
{
object thisDeserializedObject = null;
byte[] thisByteArray = Convert.FromBase64String(StringToDeserialize);
MemoryStream ms = new MemoryStream(thisByteArray);
try
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

thisDeserializedObject = formatter.Deserialize(ms);
}
catch (System.Runtime.Serialization.SerializationException e)
{
Logs.WriteToLog("Error Deserializing " + StringToDeserialize + " String:
" + e.Message);
throw;
}
finally
{
ms.Close();
}

return thisDeserializedObject;
}

Once I've serialized the object to a string I simply set the SqlParameter to
the string. Does this answer your question?

Yes, I was looking for method to serialize object of class with members
like SqlParameter, but now I think that it's not possible. Tough luck. I
use [NonSerialized] attribute and make it differently.

Thanks,
Marcin
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top