Skip to content

Instantly share code, notes, and snippets.

@AArnott
Created March 9, 2025 22:58
Show Gist options
  • Select an option

  • Save AArnott/f2e67c7dc194b587287824c56666d877 to your computer and use it in GitHub Desktop.

Select an option

Save AArnott/f2e67c7dc194b587287824c56666d877 to your computer and use it in GitHub Desktop.
AOT safe msgpack serialization with List<T>
using Nerdbank.MessagePack; // Comes from the Nerdbank.MessagePack nuget package
using PolyType;
namespace MessagePackBug;
[GenerateShape]
public partial record ItemsRequest(
[property:Key(0)]
string Prop1,
[property:Key(1)]
string Prop2,
[property:Key(2)]
List<Item> Items);
public record Item(
[property: Key(0)] string Prop1,
[property: Key(1)] string Prop2
);
class Program
{
static readonly MessagePackSerializer Serializer = new();
static void Main()
{
var req = new ItemsRequest(
"prop1",
"prop2",
[
new Item("1", "2"),
new Item("3", "4")
]);
var result = Serializer.Serialize(req);
Console.WriteLine(Convert.ToHexString(result));
}
}
@AArnott
Copy link
Copy Markdown
Author

AArnott commented Mar 9, 2025

@AlphaBs @puli1027 @Zdendaki filed or +1'd an issue on MessagePack-CSharp about a month ago asking why List<T> breaks AOT in that library.

I tried the same repro, just slightly changed to use Nerdbank.MessagePack instead, and it worked in AOT perfectly:

> bin\AotNativeConsole\Release\net9.0\win-arm64\publish\AotNativeConsole.exe
93A570726F7031A570726F70329292A131A13292A133A134

@AlphaBs
Copy link
Copy Markdown

AlphaBs commented Mar 11, 2025

thank you. it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment