SerializationException when deserialize an object
-
hello, I have developed a web service. The server (web service)serializes an object (a struct) via BinaryFormatter and then returns it to the client. The client receives the serializated object and tries to deserialize it. This throws the following exception: System.Runtime.Serialization.SerializationException: Bynary stream does not contain a valid BinaryHeader, 0 possible causes, invalid stream or object version change between serialization and deserialization. Here is the code: serialization:
MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms,myStruct); byte[] bArray = ms.GetBuffer(); ms.Close();
deserialization:MemoryStream ms = new MemoryStream(bArray); BinaryFormatter bf =new BinaryFormatter(); ms.Position = 0; myStruct struct =(myStruct)bf.Deserialize(ms); ms.Close();
Do you know what is happening? thank you. -
hello, I have developed a web service. The server (web service)serializes an object (a struct) via BinaryFormatter and then returns it to the client. The client receives the serializated object and tries to deserialize it. This throws the following exception: System.Runtime.Serialization.SerializationException: Bynary stream does not contain a valid BinaryHeader, 0 possible causes, invalid stream or object version change between serialization and deserialization. Here is the code: serialization:
MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms,myStruct); byte[] bArray = ms.GetBuffer(); ms.Close();
deserialization:MemoryStream ms = new MemoryStream(bArray); BinaryFormatter bf =new BinaryFormatter(); ms.Position = 0; myStruct struct =(myStruct)bf.Deserialize(ms); ms.Close();
Do you know what is happening? thank you.It appears you have an assembly version mismatch. Resolution: Create a seperate assembly u can both reference from the service and the client (i tend to like give that a fixed version number, not the auto one). Another problem u could have, that I have seen, is closing a MemoryStream after putting an image thru, causes problems, not sure if this a bug or not, as calling Dispose via an implicit interface call infact works. top secret xacc-ide 0.0.1
-
It appears you have an assembly version mismatch. Resolution: Create a seperate assembly u can both reference from the service and the client (i tend to like give that a fixed version number, not the auto one). Another problem u could have, that I have seen, is closing a MemoryStream after putting an image thru, causes problems, not sure if this a bug or not, as calling Dispose via an implicit interface call infact works. top secret xacc-ide 0.0.1
-
It appears you have an assembly version mismatch. Resolution: Create a seperate assembly u can both reference from the service and the client (i tend to like give that a fixed version number, not the auto one). Another problem u could have, that I have seen, is closing a MemoryStream after putting an image thru, causes problems, not sure if this a bug or not, as calling Dispose via an implicit interface call infact works. top secret xacc-ide 0.0.1
hello, I think I have found what is the problem... but I do not know the solution. The problem is that I am using CAPICOM.SignedData. The process is as follows: 1.- Serialize the struct. 2.- Convert the byte array into a string via System.Text.Encoding.UTF8.GetString() method. 3.- sign the data (the string) with SignedData.sign() method and return the result (a string with a PKCS#7 structure) to the client. 4.- The client verifies the signed data and extracts the SignedData content (a string) which is the serialized struct. 5.- Finally, the client tries to deserialize the struct and it throws the exception. I think the problem is the SignedData operations because without them I can serialize and deserialize the structs without any problem. Any suggestions? thank you.
-
It appears you have an assembly version mismatch. Resolution: Create a seperate assembly u can both reference from the service and the client (i tend to like give that a fixed version number, not the auto one). Another problem u could have, that I have seen, is closing a MemoryStream after putting an image thru, causes problems, not sure if this a bug or not, as calling Dispose via an implicit interface call infact works. top secret xacc-ide 0.0.1
hello, i have solved partially my problem: -- The problem with the signedData was the conversion from byte array to string: i was converting the byte array (the serialized object) into a UTF8 string instead an Unicode string. So, the solution was to use "System.Text.Encoding.Unicode.GetString()" -- but now, I have another problem. When the client tries to deserialize the struct it gets the following message exception: System.IO.FileNotFoundException: File or assembly name WEBSERVICE, or one of its dependencies, was not found. I have develop a testing Console Application that serializes an object, signs it and verifies it with SignedData, and finally, deserializes it and it works well. So, I think it is a WebServices problem. It sounds like client can not find the serialized object class or the namespace where it is contained but this is rare for me because the client can instanciate that class. thank you one more time:-D
-
hello, "Create a seperate assembly u can both reference from the service and the client (i tend to like give that a fixed version number, not the auto one)." Could you give me a reference (article, tutorial, book...) to learn how to do it? Thank you.
This is basic. Create another assembly that defines the type. Both the client and server project depend on this assembly and use the type defined in that shared assembly. As far as fixing the version number, go to your AssemblyInfo.cs file and change the
AssemblyVersionAttibute
value to something fixed (no asterisk - *). If you use an asterisk, the assembly version is automatically generated based on the documentation for theAssemblyVersionAttribute
in the .NET Framework SDK. This is a terrible feature that wasn't meant to be in the .NET Framework originally, or so I hear. .NET assemblies link against versions of other assemblies. Changing the version breaks this linkage, but there are ways to redirect assembly versions. Read Redirecting Assembly Versions[^] in the .NET Framework SDK for more information.Microsoft MVP, Visual C# My Articles