Skip to content

Many built-in formatters fail when referenced from MessagePackFormatterAttribute #1328

Closed

Description

@Miyuyami

Framework: .NET 5
MessagePack: 2.3.75
I have the following example object:

public enum PacketType : ushort
{
    TestZero = 0,
    Login = 1,
    PlayerData = 2,
}

[MessagePackObject]
public class Packet
{
    [Key(0)] public uint Length { get; set; }
    [Key(1)] public ushort Type { get; set; }
    [Key(2)] public PacketType TypeEnum { get; set; }
    [Key(3)] public byte Value { get; set; }
}

var packet = new Packet()
{
    Length = 9,
    Type = 0x12,
    TypeEnum = PacketType.PlayerData,
    Value = 0x29,
};
using var stream = new MemoryStream();
MessagePackSerializer.Serialize(stream, packet);

Console.WriteLine($"[{String.Join(", ", stream.ToArray().Select(b => $"0x{b:X2}"))}]");

I want to serialize it into:
[0x09, 0x00, 0x00, 0x00, 0x12, 0x00, 0x02, 0x00, 0x29]
but I get
[0x94, 0x09, 0x12, 0x02, 0x29]

I tried using the Force Block Formatter attributes like this:

[MessagePackObject]
public class Packet
{
    [Key(0)] [MessagePackFormatter(typeof(ForceUInt32BlockFormatter))] public uint Length { get; set; }
    [Key(1)] [MessagePackFormatter(typeof(ForceUInt16BlockFormatter))] public ushort Type { get; set; }
    [Key(2)] [MessagePackFormatter(typeof(ForceUInt16BlockFormatter))] public PacketType TypeEnum { get; set; }
    [Key(3)] [MessagePackFormatter(typeof(ForceByteBlockFormatter))] public byte Value { get; set; }
}

But I'm getting the following exception:

MessagePack.MessagePackSerializationException
  HResult=0x80131500
  Message=Failed to serialize Example.Packet value.
  Source=MessagePack
  StackTrace:
   at MessagePack.MessagePackSerializer.Serialize[T](MessagePackWriter& writer, T value, MessagePackSerializerOptions options)
   at MessagePack.MessagePackSerializer.Serialize[T](IBufferWriter`1 writer, T value, MessagePackSerializerOptions options, CancellationToken cancellationToken)
   at MessagePack.MessagePackSerializer.Serialize[T](Stream stream, T value, MessagePackSerializerOptions options, CancellationToken cancellationToken)
   at Example.Tests.ExampleTestClass.ExampleTestMethod()

  This exception was originally thrown at this call stack:
    System.RuntimeType.CreateInstanceImpl(System.Reflection.BindingFlags, System.Reflection.Binder, object[], System.Globalization.CultureInfo)
    System.Activator.CreateInstance(System.Type, System.Reflection.BindingFlags, System.Reflection.Binder, object[], System.Globalization.CultureInfo, object[])
    MessagePack.Formatters.Example_PacketFormatter1.Example_PacketFormatter1()

Inner Exception 1:
TypeInitializationException: The type initializer for 'FormatterCache`1' threw an exception.

Inner Exception 2:
TargetInvocationException: Exception has been thrown by the target of an invocation.

Inner Exception 3:
MissingMethodException: Constructor on type 'MessagePack.Formatters.ForceUInt32BlockFormatter' not found.

Implementing my own Formatters with a public constructor gives a different exception:

MessagePack.MessagePackSerializationException
  HResult=0x80131500
  Message=Failed to serialize Example.Packet value.
  Source=MessagePack
  StackTrace:
   at MessagePack.MessagePackSerializer.Serialize[T](MessagePackWriter& writer, T value, MessagePackSerializerOptions options)
   at MessagePack.MessagePackSerializer.Serialize[T](IBufferWriter`1 writer, T value, MessagePackSerializerOptions options, CancellationToken cancellationToken)
   at MessagePack.MessagePackSerializer.Serialize[T](Stream stream, T value, MessagePackSerializerOptions options, CancellationToken cancellationToken)
   at Example.Tests.ExampleTestClass.ExampleTestMethod()

Inner Exception 1:
EntryPointNotFoundException: Entry point was not found.

I've also tried to set different Resolvers in MessagePackSerializerOptions but had same exception.
Is it possible to use MessagePack for creating fixed packet sizes and not compact any of the data? I need it to communicate with a TCP server that I cannot control.

Activity

  1. changed the title [-]Force fixed size block formatting [question][/-] [+][Question] Force fixed size block formatting[/+] on Sep 19, 2021
  2. AArnott commented on Sep 19, 2021

    @AArnott
    Collaborator

    I've no idea what the "Entry point was not found." exception is all about, but the one you mention with the built-in block formatters is probably due to a regression in the library, which I may be able to work on fixing this week.

  3. self-assigned this
    on Sep 19, 2021
  4. added this to the v2.3 milestone on Sep 22, 2021
  5. AArnott commented on Sep 22, 2021

    @AArnott
    Collaborator

    The MessagePackFormatterAttribute requires the formatter to offer a public default constructor. I wrote a test to check that all formatters in the library conform to this requirement and it fails on 90 of 172 formatters. These non-conforming formatters instead use a singleton pattern. We could add public constructors for all of them, or we could teach the library to support the singleton pattern where the formatter supports it so we have fewer instantiated copies of identical formatters. I'm leaning toward the latter.
    @neuecc any concerns or alternate suggestions?

  6. AArnott commented on Sep 22, 2021

    @AArnott
    Collaborator

    @Miyuyami Your EntryPointNotFoundException happened to hit me too. It was because the formatter I specified did not precisely match the type of the field I attributed. For example I used [MessagePackFormatter(typeof(ForceUInt64BlockFormatter))] on a long when that particular formatter takes a ulong. So make sure it precisely matches and you should be fine.
    With my fix you should be able to use our built-in block formatters (with precise type matching still) rather than write your own.

  7. changed the title [-][Question] Force fixed size block formatting[/-] [+]Many built-in formatters fail when referenced from `MessagePackFormatterAttribute`[/+] on Sep 22, 2021
  8. Miyuyami commented on Sep 22, 2021

    @Miyuyami
    Author

    Thank you 🙂! EntryPointNotFoundException seems to have happened because of the PacketType enum I used and UInt16 formatter doesn't match it like you mentioned.

    Is it possible to serialize/deserialize in little endian and without the message pack codes?
    For example if I want to serialize/deserialize this object:

    [MessagePackObject]
    public class Packet
    {
        [Key(0)] [MessagePackFormatter(typeof(ForceUInt32BlockFormatter))] public uint Length { get; set; }
        [Key(1)] [MessagePackFormatter(typeof(ForceUInt16BlockFormatter))] public ushort Type { get; set; }
        [Key(2)] [MessagePackFormatter(typeof(ForceByteBlockFormatter))] public byte Value { get; set; }
    }
    var packet = new Packet()
    {
        Length = 9,
        Type = 0x12,
        Value = 0x29,
    };

    I would want to serialize it into:
    0x09, 0x00, 0x00, 0x00, 0x12, 0x00, 0x29

  9. added a commit that references this issue on Sep 22, 2021
    9f4c6d7
  10. AArnott commented on Sep 22, 2021

    @AArnott
    Collaborator

    You want little endian, and without the msgpack codes? No, that would violate the msgpack spec which explicitly calls for big endian. If you want little endian, it isn't messagepack and you'd be better off writing your own serializer.

  11. Miyuyami commented on Sep 22, 2021

    @Miyuyami
    Author

    Ah, I understand. I was looking through multiple libraries but only MessagePack looked promising because it looked like it could be customised to even go off-spec.
    Do you know any similar library that is able to do this?

  12. AArnott commented on Sep 22, 2021

    @AArnott
    Collaborator

    No, sorry. It sounds like you have a proprietary protocol. You'll probably have to write the code yourself. You can look at this library's source code to see how it controls endianness and you can just do the opposite.

  13. Miyuyami commented on Sep 22, 2021

    @Miyuyami
    Author

    Ok, thank you for the help. 😊

  14. Piguelo commented on Sep 23, 2021

    @Piguelo

    any workaround for this issue? im facing same problem.

  15. AArnott commented on Sep 23, 2021

    @AArnott
    Collaborator

    @Piguelo You can write your own formatter instead of using the built-in ones that don't work with the attribute. Your formatter could even be trivially implemented in terms of the built-in ones that just can't be directly referenced from the attribute.
    But we already have an active PR that fixes this, so it won't be long now.

  16. Piguelo commented on Sep 23, 2021

    @Piguelo

    @AArnott thank you, custom formatter does the job.

  17. added 4 commits that reference this issue on Nov 5, 2021
    cc9281e
    1c3f5a7
    988c9d4
    791450b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions