2

In my solution to serialize data with the model and stored as bit array in SQL and it retrieving the bit array and then Deserialize with the same model. In the model we have done one change. The existing property changed to shorthand property as below.

Existing code

    [Serializable()]
    public abstract class SearchResultModel : ISearchResultModel
    {
      private Guid id;
      public Guid ID
      {
        get { return this.id; }
        set { this.id = value; }
      }
    }

And its changed as below

    [Serializable()]
    public abstract class SearchResultModel : ISearchResultModel
    {
      public Guid ID { get; set; }
    }

The data is serialize and deserialize with both new and old model. But the problem is that when we retrieve old model serialized bit-array trying to deserialize with new Model the ID become GUID.Empty.

The code for serialize and deserialize has no change.

---------------For Deserialize-----

MemoryStream stream = new MemoryStream(searchResultInfoData);
stream.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
var searchResultInfo = (SearchResultModel)formatter.Deserialize(stream);
stream.Flush();
stream.Close();
return searchResultInfo;
---------------For Serialize-----
 MemoryStream stream = new MemoryStream();
 BinaryFormatter formatter = new BinaryFormatter();
 formatter.Serialize(stream, instance); //instance type is Object
 stream.Seek(0, 0);
 stream.Position = 0; //Return to start
 byte[] byteArray = stream.ToArray();
 stream.Flush();
 stream.Close();
 return byteArray;

With the both model data is already stored. How we can resolve this? Did I missing something?

Sanjeev S
  • 626
  • 1
  • 8
  • 27
  • 1
    Binary serialization requires the classes to be the same when you serialize and deserialize. You modified the classes which is giving the issue. You should has a version number of your model so you can handle changes and be backward/forward compatible. – jdweng Apr 10 '20 at 10:59
  • please provide the code used for serialization, then I would like to know how you do to serialize an instance of a class which is abstract , I deduce that you use another class – sayah imad Apr 10 '20 at 11:22
  • How we can apply version number to the class ? @jdweng – Sanjeev S Apr 10 '20 at 12:25
  • @sayahimad Added – Sanjeev S Apr 10 '20 at 12:37
  • Add a property? This is a chicken and egg problem. You need to deserialize to get the version and you can't deserialize if the class is different. So you have to organize you classes properly so you can deserialize using different versions of the data. So one solution would be to have a root class with version number and other info (like date) in the root class that never changes. Or have a base class with data that never changes and inherited classes with data that can change. – jdweng Apr 10 '20 at 12:40
  • Or not use .net binary serialization. The .net binary serialization classes aren't meant for storage, they're meant for transport between different parts of your application, such as different appdomains, or copies of your application running as separate processes or on separate machines. But dealing with versioning with these classes is a pain, precisely because they were never meant to handle long-term storage and evolution of the format. I'd recommend switching to a different binary serialization system, such as protobuf, which supports these things. – Lasse V. Karlsen Apr 10 '20 at 12:42
  • I got the problem. I thought changing the property name or add new property only have problems. But here same class only changed the property to short hand. No changes in the Class name and property name. – Sanjeev S Apr 10 '20 at 13:01
  • `BinaryFormatter` serializes **public and private fields** not **properties**. For an auto-property it serializes the secret backing field. If you change from a field to an auto-property you will need to implement `ISerializable` e.g. as shown in [Changing types during binary deserialization in C#](https://stackoverflow.com/a/30040406/3744182). See also: [What are the deficiencies of the built-in BinaryFormatter based .Net serialization?](https://stackoverflow.com/q/703073/3744182). – dbc Apr 11 '20 at 15:47

0 Answers0