Serializing entity objects binary

Posts   
 
    
knez
User
Posts: 37
Joined: 01-Nov-2004
# Posted on: 06-Apr-2005 10:24:30   

Hi!

I am trying to find out how big is one of my entity object with it's subordinate objects when serialized with binary formatter. I saved serialized object to file, but when I tried to deserialize it, I got the Exception with message "End of Stream encountered before parsing was completed". By getting that message, I am not sure anymore that I even performed serialization in proper way.

DataEntity data = DataManager.GetData(100);
FileStream writeStream = new FileStream("MyData.bin", FileMode.Create, FileAccess.Write);
IFormatter formatter = new BinaryFormatter();
try
{
    formatter.Serialize(writeStream, data);
}
catch (SerializationException ex)
{
    Debug.WriteLine(ex.Message);
}
finally
{
    writeStream.Close();
}

Stream readStream = new FileStream("MyData.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
DataEntity readData = null;
try
{
    readData = (DataEntity)formatter.Deserialize(readStream);
}
catch (SerializationException ex)
{
    Debug.WriteLine(ex.Message);
}
catch (Exception ex)
{
    Debug.WriteLine(ex.Message);
}
finally
{
    readStream.Close();
}

DataEntity is entity object generated from template provided with LLBLGen Pro 1.2004.1.

Can anyone tell me where the problem is?

Thanks.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 06-Apr-2005 10:45:52   

Stream readStream = new FileStream("MyData.bin", FileMode.Open, FileAccess.Read, FileShare.Read);

Shouldn't that be: FileStream readStream = new FileStream("MyData.bin", FileMode.Open, FileAccess.Read, FileShare.Read);

Though, your code should work fine.

However if you just want to know the size, you can also use a MemoryStream, saves you the saving to disk simple_smile

Frans Bouma | Lead developer LLBLGen Pro
knez
User
Posts: 37
Joined: 01-Nov-2004
# Posted on: 06-Apr-2005 13:35:12   

Otis wrote:

Though, your code should work fine.

No, unfortunately it doesn't cry . I wrote simple Console application with entity classes generated from Northwind database using Adapter scenario. I switched to MemoryStream.

MemoryStream stream = new MemoryStream(100000);
IFormatter formatter = new BinaryFormatter();
CustomersEntity customer = GetCustomer("COMMI"); // Gets CustomersEntity without subordinate objects.
try
{
    formatter.Serialize(stream, customer) ;
}
catch (Exception ex)
{
    Console.WriteLine(String.Format("Serialization error: {0}", ex.Message));
}
Console.WriteLine(String.Format("Stream size {0}", stream.Length));

CustomersEntity readCustomer = null;
try
{
    readCustomer = (CustomersEntity) formatter.Deserialize(stream);
}
catch (Exception ex)
{
    Console.WriteLine(String.Format("Deserialization error {0}", ex.Message));
}
stream.Close();

I still get the same exception while deserializing object from stream ("End of Stream encountered before parsing was completed."). MemoryStream has length of 3684, so created stream has capacity that is large enough.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 06-Apr-2005 14:00:36   

knez wrote:

Otis wrote:

Though, your code should work fine.

No, unfortunately it doesn't cry . I wrote simple Console application with entity classes generated from Northwind database using Adapter scenario. I switched to MemoryStream.

MemoryStream stream = new MemoryStream(100000);
IFormatter formatter = new BinaryFormatter();
CustomersEntity customer = GetCustomer("COMMI"); // Gets CustomersEntity without subordinate objects.
try
{
    formatter.Serialize(stream, customer) ;
}
catch (Exception ex)
{
    Console.WriteLine(String.Format("Serialization error: {0}", ex.Message));
}
Console.WriteLine(String.Format("Stream size {0}", stream.Length));

CustomersEntity readCustomer = null;
try
{
    readCustomer = (CustomersEntity) formatter.Deserialize(stream);
}
catch (Exception ex)
{
    Console.WriteLine(String.Format("Deserialization error {0}", ex.Message));
}
stream.Close();

I still get the same exception while deserializing object from stream ("End of Stream encountered before parsing was completed."). MemoryStream has length of 3684, so created stream has capacity that is large enough.

Ah, but that's caused because you have to seek to the beginning of the stream again: stream.Seek(0, SeekOrigin.Begin);

As with the current code, the file pointer is at the end of the stream.

Frans Bouma | Lead developer LLBLGen Pro
knez
User
Posts: 37
Joined: 01-Nov-2004
# Posted on: 06-Apr-2005 14:27:45   

It works! But, the first example I wrote used separate streams for writing and reading (and I even tried with setting pointer to beginning of stream - as not seen in example).

Nevertheless, I did find how big object(s) is.

Once again, thanks.