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. Correct deserialization with BinaryFormatter

Correct deserialization with BinaryFormatter

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 2 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.
  • C Offline
    C Offline
    Chesnokov Yuriy
    wrote on last edited by
    #1

    How to check Stream contains valid BinaryHeader to avoid deserialization exceptions such as Binary stream '0' does not contain a valid BinaryHeader?

    Чесноков

    P 1 Reply Last reply
    0
    • C Chesnokov Yuriy

      How to check Stream contains valid BinaryHeader to avoid deserialization exceptions such as Binary stream '0' does not contain a valid BinaryHeader?

      Чесноков

      P Offline
      P Offline
      Paw Jershauge
      wrote on last edited by
      #2

      Could you post some code ??? so we can see what your trying to do. ?

      With great code, comes great complexity, so keep it simple stupid...:-\ :-\

      C 1 Reply Last reply
      0
      • P Paw Jershauge

        Could you post some code ??? so we can see what your trying to do. ?

        With great code, comes great complexity, so keep it simple stupid...:-\ :-\

        C Offline
        C Offline
        Chesnokov Yuriy
        wrote on last edited by
        #3

        I do not want that deserialization call formatter.Deserialize(ms); will result in Exception. Thus I need to check that MemoryStream ms; contains valid header which is used internally in BinaryFormatter.Deserialize() call.

        Чесноков

        P 1 Reply Last reply
        0
        • C Chesnokov Yuriy

          I do not want that deserialization call formatter.Deserialize(ms); will result in Exception. Thus I need to check that MemoryStream ms; contains valid header which is used internally in BinaryFormatter.Deserialize() call.

          Чесноков

          P Offline
          P Offline
          Paw Jershauge
          wrote on last edited by
          #4

          Its not that simple. please provide an sample code from where you get the error. else im unable to help you.

          With great code, comes great complexity, so keep it simple stupid...:-\ :-\

          C 1 Reply Last reply
          0
          • P Paw Jershauge

            Its not that simple. please provide an sample code from where you get the error. else im unable to help you.

            With great code, comes great complexity, so keep it simple stupid...:-\ :-\

            C Offline
            C Offline
            Chesnokov Yuriy
            wrote on last edited by
            #5

            It is very simple, MemoryStream contains your data for deserialization. With MemoryStream.GetBuffer(); you may get access to underlying byte[] array. It needs to check that in that array first bytes are valid BinaryHeader structure used inernally by BinaryFormatter during deserialization call. If there is garbage in the MemoryStream then BinaryFormatter will throw exception. I need to avoid that exception and make sure correct data will be passed for deserialization. Finally I found that link that resolves the issue http://primates.ximian.com/~lluis/dist/binary_serialization_format.htm[^]

            Чесноков

            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