"No parameterless constructor defined for type..." when serializing record with List<T>, on AOT compiled binary
還沒有人認領這個 Issue。
評估
研究方向
Reproduce the failure using the provided record types and the dotnet publish command with PublishAOT enabled. Start at DynamicGenericResolverGetFormatterHelper.GetFormatter and DynamicGenericResolver.FormatterCache in the stack trace, then inspect how GeneratedMessagePackResolver.ItemsRequestFormatter resolves List. Done means the sample serializes successfully in the AOT-published binary without the missing-constructor exception.
由索引模型根據 Issue 內容生成。
描述
Bug description
When AOT-compiled, serializing a record with a List property throws a MessagePackSerializationException indicating there's "No parameterless constructor defined for type 'MessagePack.Formatters.ListFormatter`1[...]'".
Repro steps
using System.Text.Json.Serialization;
using MessagePack;
namespace MessagePackBug;
[MessagePackObject]
public record ItemsRequest(
[property:Key(0)]
string Prop1,
[property:Key(1)]
string Prop2,
[property:Key(2)]
List<Item> Items);
[MessagePackObject]
public record Item(
[property:Key(0)] string Prop1,
[property:Key(1)] string Prop2
);
class Program
{
static void Main()
{
var req = new ItemsRequest(
"prop1",
"prop2",
[
new Item("1", "2"),
new Item("3", "4")
]);
var result = MessagePackSerializer.Serialize(req);
Console.WriteLine(Convert.ToHexString(result));
}
}
compile with AOT option and run. I used following script:
dotnet publish --self-contained=true -o "bin/publish-product"
.\bin\publish-product\MessagePackBug.exe
Expected behavior
serialize without any error
Actual behavior
Restore complete (0.2s)
MessagePackBug succeeded with 2 warning(s) (7.5s) → bin\publish-product\
C:\Users\ksi12\.nuget\packages\messagepack\3.1.2\lib\net9.0\MessagePack.dll : warning IL3053: Assembly 'MessagePack' produced AOT analysis warnings.
C:\Users\ksi12\.nuget\packages\messagepack\3.1.2\lib\net9.0\MessagePack.dll : warning IL2104: Assembly 'MessagePack' produced trim warnings. For more information see https://aka.ms/il2104
Build succeeded with 2 warning(s) in 8.0s
Unhandled exception. MessagePack.MessagePackSerializationException: Failed to serialize MessagePackBug.ItemsRequest value.
---> System.TypeInitializationException: A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
---> System.MissingMethodException: No parameterless constructor defined for type 'MessagePack.Formatters.ListFormatter`1[MessagePackBug.Item]'.
at System.ActivatorImplementation.CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo, Object[]) + 0x3b7
at MessagePack.Internal.DynamicGenericResolverGetFormatterHelper.GetFormatter(Type) + 0x546
at MessagePack.Resolvers.DynamicGenericResolver.FormatterCache`1..cctor() + 0x23
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xb6
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x13c
at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0xd
at MessagePack.Resolvers.DynamicGenericResolver.GetFormatter[T]() + 0x11
at MessagePack.Resolvers.StandardResolver.FormatterCache`1..cctor() + 0xa4
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xb6
--- End of stack trace from previous location ---
at MessagePack.FormatterResolverExtensions.Throw(TypeInitializationException ex) + 0x21
at MessagePack.FormatterResolverExtensions.GetFormatterWithVerify[T](IFormatterResolver resolver) + 0x9e
at MessagePack.GeneratedMessagePackResolver.MessagePackBug.ItemsRequestFormatter.Serialize(MessagePackWriter&, ItemsRequest, MessagePackSerializerOptions) + 0x112
at MessagePack.MessagePackSerializer.Serialize[T](MessagePackWriter& writer, T value, MessagePackSerializerOptions options) + 0xd3
--- End of inner exception stack trace ---
at MessagePack.MessagePackSerializer.Serialize[T](MessagePackWriter& writer, T value, MessagePackSerializerOptions options) + 0x26e
at MessagePack.MessagePackSerializer.Serialize[T](T value, MessagePackSerializerOptions options, CancellationToken cancellationToken) + 0x19d
at MessagePackBug.Program.Main() + 0xfe
- Version used: MessagePack 3.1.2
- Runtime: .NET 9.0 with PublishAOT option
Additional context
Add any other context about the problem here.
- 主要語言
- C#
- 星號
- 6.8k
- 分支
- 778
- 平均合併
- 22 小時 8 分鐘
- 30 天內合併 PR
- 2
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
MessagePack-CSharp/MessagePack-CSharp 的其他 Issue
-
SerializableMemberTypeAnalyzer throws AD0001 when any referenced assembly has an assembly-level attribute with a single array argument可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 2/5 1-3 小時 新手友好度 82/100
-
難度 1/5 1 小時以內 新手友好度 76/100
-
難度 2/5 1-3 小時 新手友好度 68/100
MessagePack-CSharp/MessagePack-CSharp#2276 · 2 則留言 ·
-
難度 2/5 1-3 小時 新手友好度 66/100
MessagePack-CSharp/MessagePack-CSharp#2252 · 3 則留言 ·
-
難度 4/5 3-5 天 新手友好度 52/100
查看 MessagePack-CSharp/MessagePack-CSharp 的全部 Issue
相似的 Issue
-
kind/bug
難度 2/5 1-3 小時 新手友好度 78/100
unoplatform/uno#26555 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 75/100
Listenarrs/Listenarr#1173 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 77/100
CactuseSecurity/firewall-orchestrator#5739 ·
維護者通常 1 天內回覆
-
chore project-system small
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 68/100
vikramlearning/blazorbootstrap#1388 ·
維護者通常 1 天內回覆