BinaryFormatter -> Invalid binaryHeader
-
Hello, I have a strange problem. I want to send some objectes over Ethernet (UDP One packet is one object) After that I Deserialize the data, but I get allways an exception that there is an Invalid binaryheader. I don't have any idea how I can solve it. my code is
int size = socket.Receive(buf); BinaryFormatter serializer = new BinaryFormatter(); MemoryStream ms = new MemoryStream(buf,0,size); ms.Seek(0, SeekOrigin.Begin); LogMessage logMessage = (LogMessage)serializer.Deserialize(ms); Console::WriteLine("{0}", logMessage.ToString()); ms.Close();
and the sending function:MemoryStream ms = new MemoryStream(); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(ms, logMessage); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); byte[] data = ms.ToArray(); socket.Send(data); ms.Close();
I have checked the MemoryStream and there is the whole data available (Sending and receiving) LogMessage Best regards Hansjörg -
Hello, I have a strange problem. I want to send some objectes over Ethernet (UDP One packet is one object) After that I Deserialize the data, but I get allways an exception that there is an Invalid binaryheader. I don't have any idea how I can solve it. my code is
int size = socket.Receive(buf); BinaryFormatter serializer = new BinaryFormatter(); MemoryStream ms = new MemoryStream(buf,0,size); ms.Seek(0, SeekOrigin.Begin); LogMessage logMessage = (LogMessage)serializer.Deserialize(ms); Console::WriteLine("{0}", logMessage.ToString()); ms.Close();
and the sending function:MemoryStream ms = new MemoryStream(); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(ms, logMessage); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); byte[] data = ms.ToArray(); socket.Send(data); ms.Close();
I have checked the MemoryStream and there is the whole data available (Sending and receiving) LogMessage Best regards HansjörgAre you serializing and deserializing using the same .NET Frameowrk / Operating System? If you are serializing in, for instance, .NET Framework and the deserializing in .NET Compact Framework, this will be an issue. Otherwise, you can use the XML Serializer, if platform/framework is your problem
-
Are you serializing and deserializing using the same .NET Frameowrk / Operating System? If you are serializing in, for instance, .NET Framework and the deserializing in .NET Compact Framework, this will be an issue. Otherwise, you can use the XML Serializer, if platform/framework is your problem
-
I use the same computer, same plattform and the both projects are in the same solution from vs2005.. I want to use the binary formatter, because I think it is faster and this is needed in my case. Best regards Hansjörg
Maybe it is a good idea to use the SOAP serializer first for debugging purposes. It is easier to review to XML then. It is not very difficult to switch back to the binary serializer once it all works.
-
Hello, I have a strange problem. I want to send some objectes over Ethernet (UDP One packet is one object) After that I Deserialize the data, but I get allways an exception that there is an Invalid binaryheader. I don't have any idea how I can solve it. my code is
int size = socket.Receive(buf); BinaryFormatter serializer = new BinaryFormatter(); MemoryStream ms = new MemoryStream(buf,0,size); ms.Seek(0, SeekOrigin.Begin); LogMessage logMessage = (LogMessage)serializer.Deserialize(ms); Console::WriteLine("{0}", logMessage.ToString()); ms.Close();
and the sending function:MemoryStream ms = new MemoryStream(); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(ms, logMessage); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); byte[] data = ms.ToArray(); socket.Send(data); ms.Close();
I have checked the MemoryStream and there is the whole data available (Sending and receiving) LogMessage Best regards HansjörgI have seen some funny stuff when closing
MemoryStream
, remove those .Close() for now. If that is the problem, what does work is placing it in ausing
block.**
xacc.ide-0.2.0.74 - now with C# 3.5 support!
**
-
I have seen some funny stuff when closing
MemoryStream
, remove those .Close() for now. If that is the problem, what does work is placing it in ausing
block.**
xacc.ide-0.2.0.74 - now with C# 3.5 support!
**
-
Maybe it is a good idea to use the SOAP serializer first for debugging purposes. It is easier to review to XML then. It is not very difficult to switch back to the binary serializer once it all works.
with the soap formatter I have one problem. If I serialize my object then I get a NullReferenceException. The problem it seems that it is a DateTime field. If this field is not inside the class (other data are strings) all works fine. Do you have any idea? Best regards Hansjörg
-
with the soap formatter I have one problem. If I serialize my object then I get a NullReferenceException. The problem it seems that it is a DateTime field. If this field is not inside the class (other data are strings) all works fine. Do you have any idea? Best regards Hansjörg
I'm not very experienced with this topic. A NullReferenceExpetion means that soemthing is wrong with an object. Likely causes are that you forgot to assign memory (using new) or maybe you try to use an object that no longer exists. Best way to solve this is stepping through the suspected code using the debugger and checking the object values.
-
I'm not very experienced with this topic. A NullReferenceExpetion means that soemthing is wrong with an object. Likely causes are that you forgot to assign memory (using new) or maybe you try to use an object that no longer exists. Best way to solve this is stepping through the suspected code using the debugger and checking the object values.
-
I have checked it...the object that gives the exception is a DateTime value...and this value is okay. I have no idea why... Best regards Hansjörg
I saw you posted this question in another post. There you mentioned other parameters. Did you check these as well? Also check all other variables you use. Somtimes these errors really are obscure, but it is almost certain you did something wrong. You also may try slight code variations, just to see if these work. Fi replace parameters with constant values, replace the datetime by anonther type of object etc. Just keep trying. It may take you two or three days before you find out.
-
I saw you posted this question in another post. There you mentioned other parameters. Did you check these as well? Also check all other variables you use. Somtimes these errors really are obscure, but it is almost certain you did something wrong. You also may try slight code variations, just to see if these work. Fi replace parameters with constant values, replace the datetime by anonther type of object etc. Just keep trying. It may take you two or three days before you find out.