.NET Framework Bookmark and Share   
 index > .NET Remoting and Runtime Serialization > Serialize error
 

Serialize error

Hiiii All

System.Runtime.Serialization.SerializationException: No top object.

What is scary is that it seems to happen at random with the same code (it
works most of the time, but not all of the time). Are there any known bugs
with MemoryStreams or Serialization? The code looks like this:

Public Function Read_Object_From_File(ByVal FileName As String) As Object

Dim Ob As Object

Try

Dim FStream As New IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read)

Dim Binary_Formater As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

Ob = Binary_Formater.Deserialize(FStream)

FStream.Close()

Return Ob

Catch ex As Runtime.Serialization.SerializationException

Get_Exception_Massege(ex)

Catch ex As Exception

Return Nothing

End Try

Return Nothing

End Function

Ora-dbaabode
Hiii Aaaaaaalllllllll

Please Anybady
Ora-dbaabode
Hi,
I had similar problem when I was trying to deserialize from MemoryStream, something about invalid first element.
Solution was to set location to 0, for ex

memoryStream.Location=0;

and then call deserialize.

In your case try

FStream.Location = 0;
Ob = Binary_Formater.Deserialize(FStream);

Milan Erić
thank you milan

I will try this ,
Ora-dbaabode
hi milan

I don't find location property in memoryStream Class
Ora-dbaabode
Sorry, my bad :(
Property name is Position, not Location..
Milan Erić

I find it

Ora-dbaabode
sorry,that isn't useful
Ora-dbaabode

I had a similar problem, but I was using CryptoStream, FileStream and BinaryFormatter.
The following resolved the issue for me: prior to serializing the objects, I wrote a few characters into the stream, so I know where the beginning is. Then when I deserialize the objects I just need to read past these characters prior to beginning the deserialization.

The below examples are in c#, but you should get the idea of what I mean by writing a few characters into the stream.

//Below is a snippet from my serilization method

fs = new FileStream(mySavePath, FileMode.Create);
fs.Write(Encoding.ASCII.GetBytes("CRYPT"), 0, 5);
cs = new CryptoStream(fs, desCypher.CreateEncryptor(key, iv), CryptoStreamMode.Write);
bf = new BinaryFormatter();
bf.Context = new StreamingContext(StreamingContextStates.File);
bf.AssemblyFormat = FormatterAssemblyStyle.Simple;
bf.FilterLevel = TypeFilterLevel.Full;
bf.TypeFormat = FormatterTypeStyle.TypesAlways;

bf.Serialize(cs, myListToSave);
cs.FlushFinalBlock();
//Below is a snippet from my deserilization method

bf = new BinaryFormatter();
bf.Context = new StreamingContext(StreamingContextStates.File);
bf.AssemblyFormat = FormatterAssemblyStyle.Simple;
bf.FilterLevel = TypeFilterLevel.Full;
bf.TypeFormat = FormatterTypeStyle.TypesAlways;

fs = new FileStream(mySavePath, FileMode.Open, FileAccess.Read, FileShare.Read);
fs.Position = 5;
int length = (int)fs.Length;

byte[] encryptedBytes = new byte[fs.Length];
fs.Read(encryptedBytes, 0, length - 5);
fs.Position = 5;

cs = new CryptoStream(fs, decryptor, CryptoStreamMode.Read);

tempList = (myInfoList)bf.Deserialize((Stream)cs);
jkramer001

You can use google to search for other answers

Custom Search

More Threads

• Where does one learn about remoting?
• IPC Port getting 'Access is denied'
• Delagation over the internet
• Running sample with 2 machines
• DataSet binary serialization or DataSetSurrogate
• How can i get html from aspx by programming?
• SecurityException when invoking Web Service Method that uses .Net Remoting
• How to debug remoted apps?
• Remotting vs CORBA
• How to correctly deserialize a file after a DLL has been splitted ?