0

I am trying to learn how to do binary serialziation.

This exception gets thrown every time I try to deserialize an object I previously serialized:

Binary stream '161' does not contain a valid BinaryHeader. Possible causes are invalid 
stream or object version change between serialization and deserialization.

This is the header of my class:

    [Serializable]
    public ref class Servis{
        String ^_nazivServisa;
        DateTime ^_datumOsnivanjaServisa;
        double _popustZaStalneKlijente;          
        [NonSerialized] List<Izvjestaj ^> ^_izvjestaji;
        [NonSerialized] List<StalniKlijent ^> ^_stalniKlijenti;
        [NonSerialized] List<Vozilo ^> ^_poznataVozila;

this is GetObjectData method, set as public inside the same class:

            virtual void GetObjectData(SerializationInfo^ info, StreamingContext context){
                info->AddValue("Naziv servisa", _nazivServisa);
                info->AddValue("Datum osnivanja servisa", _datumOsnivanjaServisa->ToString());
                info->AddValue("Popust za stalne klijente", _popustZaStalneKlijente);
            }

this is the constructor used for deserialization:

        protected:
        Servis(SerializationInfo ^info, StreamingContext context){
                _nazivServisa = info->GetString("Naziv servisa");
                _datumOsnivanjaServisa = Convert::ToDateTime(info->GetString("Popust za stalne klijente"));
                _popustZaStalneKlijente = info->GetDouble("Popust za stalne klijente");
                _izvjestaji=gcnew List<Izvjestaj ^>;
                _stalniKlijenti = gcnew List<StalniKlijent ^>;
                _poznataVozila = gcnew List<Vozilo ^>;
            }

And this is the block of code in which I try to open a file and deserialize it; if the file doesn't exist a new dialog is shown to input some data, and then the file is serialized

            BinaryFormatter ^b = gcnew BinaryFormatter;
        try{
            FileStream ^fs = gcnew FileStream("..\\Datoteke\\Servis.dat", FileMode::Open);              
            _servis = dynamic_cast<Servis ^>(b->Deserialize(fs));   
            fs->Close();
            }
        catch(FileNotFoundException ^exc){
            KreirajServis();
            try{
                FileStream ^fs = gcnew FileStream("..\\Datoteke\\Servis.dat", FileMode::Create);                    
                b->Serialize(fs, _servis);
                fs->Close();
                }
            }
           }


private: void KreirajServis(){
         PrvoPokretanje ^p = gcnew PrvoPokretanje();
         p->ShowDialog();
         _servis=p->dajServis();

         }

Now, the exception message says that the possible causes are invalid stream or object version change between serialization and deserialization. Since I haven't touched my class between serialization and deserialization, I'm guessing the problem is with the stream, it being invalit (or is it something else!?).

Could someone, please, point me in the right direction, it would be much appreciated. :)

irfanka
  • 129
  • 1
  • 14
  • 1
    Hmm, it looks like you are not implementing ISerializable interface: public ref class Servis: public ISerializable. Without this, BinaryFormater will not call your GetObjectData method. – Emran Dec 24 '12 at 14:51
  • Well, you are right, good sir :) Thank you very much. – irfanka Dec 26 '12 at 02:46

2 Answers2

1

DateTime is a value class. So the easiest way would be to use that sintax in Servis instand of using a handler to DateTime

teter
  • 1,468
  • 1
  • 14
  • 19
  • That was my first thought too, so I've tried putting [NonSerialized] in front of DateTime - didn't work. – irfanka Dec 24 '12 at 12:10
0

Your code example for serializing has this for writing the date/time:

info->AddValue("Datum osnivanja servisa", _datumOsnivanjaServisa->ToString());

But the deserialization code reads:

_datumOsnivanjaServisa = Convert::ToDateTime(info->GetString("Naziv servisa"));

So you're writing a value with the name "Datum osnivanja servisa", but trying to read it back with the name "Naziv servisa".

If that's how your code is written, and not just a transcription bug in your question, then it's almost certainly the cause of your problem.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • I assure you, it's not that trivial. This mistake occurred while copy-pasting. Anyway, I fixed that, and still the same problem persists. :S – irfanka Dec 24 '12 at 12:07