Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SerializationException when deserialize an object

SerializationException when deserialize an object

Scheduled Pinned Locked Moved C#
questionsysadminjsonannouncement
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pelos
    wrote on last edited by
    #1

    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.

    L 1 Reply Last reply
    0
    • P pelos

      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.

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      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

      P 3 Replies Last reply
      0
      • L leppie

        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

        P Offline
        P Offline
        pelos
        wrote on last edited by
        #3

        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.

        H 1 Reply Last reply
        0
        • L leppie

          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

          P Offline
          P Offline
          pelos
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • L leppie

            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

            P Offline
            P Offline
            pelos
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • P pelos

              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.

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              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 the AssemblyVersionAttribute 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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups