1
votes

I am trying to swap a BinaryFormatter implementation with protobuf-net. The use case is to be able to serialize and deserialize the NHibernate.Configuration object. The "configuration" object is complex and we have no ability to decorate it with attributes. We add the type to the runtime type model however the object isn't being serialized (file is 0 bytes).

First I added the Configuration type to the runtime type model:

model.Add(typeof(NHibernate.Cfg.Configuration), false)

That didn't work so I added all the properties:

model.Add(typeof(NHibernate.Cfg.Configuration), false)
     .Add("ClassMappings",
          "CollectionMappings",
          "EntityNotFoundDelegate",
          "EventListeners",
          "Imports",
          "Interceptor",
          "NamedQueries",
          "NamedSQLQueries",
          "NamingStrategy",
          "Properties",
          "SqlFunctions",
          "SqlResultSetMappings");

That still didn't work as I now receive errors such as:

No serializer defined for type: NHibernate.Mapping.PersistentClass

Do I need to add the NHibernate.Mapping.PersistentClass to the runtime type model? If so, is it mapped as a subtype or a second type. Compiling the project results in similar errors for all the following types:

RuntimeTypeModel.Default.Add(typeof(NHibernate.IInterceptor), false);
RuntimeTypeModel.Default.Add(typeof(NHibernate.Mapping.RootClass), false);
RuntimeTypeModel.Default.Add(typeof(NHibernate.Mapping.Collection), false);
RuntimeTypeModel.Default.Add(typeof(NHibernate.Cfg.INamingStrategy), false);
RuntimeTypeModel.Default.Add(typeof(NHibernate.Event.EventListeners), false);
RuntimeTypeModel.Default.Add(typeof(NHibernate.Mapping.PersistentClass), false);
RuntimeTypeModel.Default.Add(typeof(NHibernate.Proxy.IEntityNotFoundDelegate), false);

Adding these types to the runtime type model doesn't work. Errors are still raised during compilation, most notably a "subtype not known" however the exception does not tell me which type the object is a subtype of.

Is there an easier way to get protobuf-net to serialize/deserialize a 3rd party object of unknown complexity?

The motivation for this question is to replace BinaryFormatter in the following use case:
Speed Up nHibernate Startup With Object Serialization

1
Wrong tool for the job.Diego Mijelshon
have to agree with you Diego. Reverted back to BinaryFormatter for configuration serialisation.paligap

1 Answers

0
votes

of unknown complexity

no, basically. This serializer is a contract serializer, and doesn't use field-names - so it needs configuration to ensure that the data remains safe and deserializable.

Do I need to add the NHibernate.Mapping.PersistentClass to the runtime type model?

Yes

If so, is it mapped as a subtype or a second type.

That depends; is it a sub-type of another DTO used in the serialization model? Or does it stand by itself?

Errors are still raised during compilation, most notably a "subtype not known" however the exception does not tell me which type the object is a subtype of.

It should tell you what the unknown type is, though; a quick test gives me messages like:

Test 'Examples.Inheritance.UnknownSubtypeMessage' failed: System.InvalidOperationException : Unexpected sub-type: Examples.Inheritance+B

From there, and looking up Examples.Inheritance.B it should be clear what the parent type is.