Repository navigation
Closed
Description
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
[-]Force fixed size block formatting [question][/-][+][Question] Force fixed size block formatting[/+]AArnott commentedon Sep 19, 2021
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.
AArnott commentedon Sep 22, 2021
The
MessagePackFormatterAttributerequires 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?
AArnott commentedon Sep 22, 2021
@Miyuyami Your
EntryPointNotFoundExceptionhappened 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 alongwhen that particular formatter takes aulong. 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.
[-][Question] Force fixed size block formatting[/-][+]Many built-in formatters fail when referenced from `MessagePackFormatterAttribute`[/+]Miyuyami commentedon Sep 22, 2021
Thank you 🙂!
EntryPointNotFoundExceptionseems 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:
I would want to serialize it into:
0x09, 0x00, 0x00, 0x00, 0x12, 0x00, 0x29Add support for the formatter singleton pattern to `MessagePackFormat…
AArnott commentedon Sep 22, 2021
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.
Miyuyami commentedon Sep 22, 2021
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?
AArnott commentedon Sep 22, 2021
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.
Miyuyami commentedon Sep 22, 2021
Ok, thank you for the help. 😊
Piguelo commentedon Sep 23, 2021
any workaround for this issue? im facing same problem.
AArnott commentedon Sep 23, 2021
@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.
Piguelo commentedon Sep 23, 2021
@AArnott thank you, custom formatter does the job.
Add support for the formatter singleton pattern to `MessagePackFormat…
Add support for the formatter singleton pattern to `MessagePackFormat…
Add support for the formatter singleton pattern to `MessagePackFormat…
Add support for the formatter singleton pattern to `MessagePackFormat…