2

I'm trying to port deserialization to protobuf-net. But I cannot get past this point:

[DataContract]
public abstract class BaseObject : IBaseObject, INotifyPropertyChanged
{
   // only methods and events here
}

[DataContract]
public abstract class BaseTableContract<TSelf> : BaseObject, ITableContract<TSelf>
  where TSelf : BaseTableContract<TSelf>, new()
{
   // only methods and events here
}

[DataContract]
public abstract class BaseTableSharedContract<TSelf, TIdentifier> : BaseTableContract<TSelf>, ITableSharedContract<TSelf, TIdentifier>
  where TSelf : BaseTableSharedContract<TSelf, TIdentifier>, new()
  where TIdentifier : IComparable
{
    [DataMember(Order = 1)] public TIdentifier ID { get; set; }
    [DataMember(Order = 2)] public DateTime Inserted { get; set; }
    [DataMember(Order = 3)] public DateTime? Updated { get; set; }
}

[DataContract]
public abstract class BaseDataTableContract<TSelf> : BaseTableSharedContract<TSelf, Guid>, IDataTableContract<TSelf>
  where TSelf : BaseDataTableContract<TSelf>, new()
{
    [DataMember(Order = 4)] public Guid IdUserInserted { get; set; }
    [DataMember(Order = 5)] public Guid? IdUserUpdated { get; set; }
    [DataMember(Order = 6)] public String Description { get; set; }
}

[DataContract]
public class FinalContract : BaseDataTableContract<FinalContract>
{
    [DataMember(Order = 7)] public Guid SomeProperty { get; set; }
}

I then register the structure (well before any serialization happens) of inheritance in run-time (both client/server) as such:

MetaType metaBase = RuntimeTypeModel.Default[typeof(BaseObject)];
metaBase.UseConstructor = false;
MetaType metaTable = metaBase.AddSubType(1, typeof(BaseTableContract<FinalContract>);
metaTable.UseConstructor = false;
MetaType metaShared = metaTable.AddSubType(2, typeof(BaseTableSharedContract<FinalContract, Guid>);
metaShared.UseConstructor = false;
MetaType metaData = metaShared.AddSubType(3, typeof(BaseDataTableContract<FinalContract>);
metaData.UseConstructor = false;
metaData.AddSubType(4, typeof(FinalContract));

Without the UseConstructor property set to false it throws it cannot find parameter-less constructor. Which is correct, there's no explicit constructor (other than the one created automatically by C#). So I turned it off (false), but it still throws in deserialization:

stream.Seek(0, SeekOrigin.Begin);
TResult result = ProtoBuf.Serializer.Deserialize<TResult>(stream);

Exception: Cannot create an abstract class

The serialization seems to work (17kB od data). I can check the inheritance structure in RuntimeTypeModel before deserialization. What am I doing wrong here?

SmartK8
  • 2,616
  • 1
  • 29
  • 37

1 Answers1

0

The problem was on our side - probably. We're calculating positive number hash (int) of the full type name (string) to be used as a sub type unique code (in AddSubType() method). This somehow doesn't work. When replaced with numbers indexed from 100 (for example), it works.

We've found out by try to compile example I gave and it worked (after minor changes to be compilable). When we used our full class hierarchy 1:1 it thrown that error. After we commented out all the insides except DataMember properties. Only after changing hash method to linear sequence it worked. Of course now it is sensitive for a server and client to contain same order of loading. But this is not a problem anymore.

I hope this will help someone in similar situation.

SmartK8
  • 2,616
  • 1
  • 29
  • 37