Messagepack-csharp: Unable to serialize interface types using contractless or typeless

Created on 3 Jul 2020  路  4Comments  路  Source: neuecc/MessagePack-CSharp

Bug description

I'm unable to serialize undecorated classes containing interface types using contractless or typeless serialization, getting:

MessagePack.MessagePackSerializationException: Failed to serialize Acme.TestClass value. ---> MessagePack.FormatterNotRegisteredException: Acme.IScheduler is not registered in resolver: MessagePack.Resolvers.CompositeResolver+CachingResolver
  at at MessagePack.FormatterResolverExtensions.Throw(Type t, IFormatterResolver resolver)
  at at Serialize(Byte[][] , Object[] , MessagePackWriter& , TestClass , MessagePackSerializerOptions )
  at at MessagePack.Internal.AnonymousSerializableFormatter`1.Serialize(MessagePackWriter& writer, T value, MessagePackSerializerOptions options)
  at at MessagePack.MessagePackSerializer.Serialize[T](MessagePackWriter& writer, T value, MessagePackSerializerOptions options)

Repro steps

Consider this class:
```c#
public class TestClass
{
public IScheduler Scheduler { get; set; }
}

public interface IScheduler
{
    Guid Id { get; set; }
}

public class SchedulerA : IScheduler
{
    public Guid Id { get; set; } = Guid.NewGuid();
}
And then attempting to serialize:
```c#
var test = new TestClass();
test.Scheduler = new SchedulerA();

// All these throw
var result1 = MessagePackSerializer.Typeless.Serialize(test);
var result2 = MessagePackSerializer.Serialize(test, MessagePack.Resolvers.ContractlessStandardResolver.Options);
var result3 = MessagePackSerializer.Serialize(test, MessagePack.Resolvers.TypelessContractlessStandardResolver.Options);

// Also throws
var resolver = CompositeResolver.Create(ContractlessStandardResolver.Instance, StandardResolver.Instance);
var options = MessagePackSerializerOptions.Standard.WithResolver(resolver);
var result4 = MessagePackSerializer.Serialize(test, options);

Expected behavior

I'd expect this to fail with the standard resolver since the types are not decorated with MessagePack attributes), but to suceed with contractless and/or typeless.

Actual behavior

MessagePackSerializationException when calling Serialize

  • Version used: tried both 2.1.115 and 2.1.152
  • Runtime: .NET Core 3.1

Most helpful comment

Ability for contractless ser/deser of interface-, abstract-typed, and parent-class-typed properties seems like a critical feature.

All 4 comments

Ability for contractless ser/deser of interface-, abstract-typed, and parent-class-typed properties seems like a critical feature.

Is there any way to achieve this? We wanted to switch from JSON to msgpack for saving app state but this is blocking us.
Since MessagePack has ability to record types during serialization it seems it could detect concrete types used in place of interfaces and derived classes and record those rather than what's known statically. Same goes for deserialization, use recorded types and not statically known through <T>

I have similar issue when migrating from NetDataContractSerializer.

Thank you for request and votes.

I've sent fix for this issue. #1078
Typeless support to serialize/deserialize interface/abstract object which not implements UnionAttribute.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MetaCitizenOffical picture MetaCitizenOffical  路  3Comments

HardcodedNumber picture HardcodedNumber  路  6Comments

ns-88 picture ns-88  路  3Comments

thorgeirk11 picture thorgeirk11  路  8Comments

williamkmlau picture williamkmlau  路  3Comments