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.
Framework: .NET 5
MessagePack: 2.3.75
I have the following example object:
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:
But I'm getting the following exception:
Implementing my own Formatters with a public constructor gives a different exception:
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.