Repository navigation
Description
Description
On .NET 11, System.Text.Json deserialization fails for certain init-only struct fields in Blazor WASM AOT mode if JsonSerializationContext is supplied.
Reproduction Steps
- Create a new Standalone Blazor WebAssembly application from template targeting .NET 11
- Enable WASM AOT publishing by adding
<RunAOTCompilation>true</RunAOTCompilation>property to the project file - Replace the content of
Pages/Home.razorwith the following sample code:
@page "/"
@using System.Text.Json
@using System.Text.Json.Serialization
<p>@obj</p>
@code {
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
string json = @"{ ""value"": ""2026-11-16T00:00:00Z"" }";
var model = JsonSerializer.Deserialize(json, ModelSerializerContext.Default.FooDateTimeOffset);
obj = model.Value;
}
public struct Foo<T>
{
[JsonPropertyName("value")]
public T Value { get; init; }
}
[JsonSerializable(typeof(Foo<DateTimeOffset>))]
internal sealed partial class ModelSerializerContext : JsonSerializerContext { }
private object? obj = null;
}- Publish the application to a folder. This is important because AOT compilation only happens on publish. Running directly, even in release mode and without debugger, does not trigger AOT build.
- Serve the published website in
wwwrootwithdotnet serveor other similar tools - Load the served website
Expected behavior
Website correctly loads and shows the deserialized DateTimeOffset object.
Actual behavior
Exception is throwed on serialization:
Unhandled exception rendering component: Attempting to JIT compile method '(wrapper other) void BlazorWasmJsonRepro.Pages.Home/ModelSerializerContext/__GenericAccessors_FooDateTimeOffset_0
1<System.DateTimeOffset>:__set_FooDateTimeOffset_Value (BlazorWasmJsonRepro.Pages.Home/Foo1<System.DateTimeOffset>&,System.DateTimeOffset)' while running in aot-only mode. See https://learn.microsoft.com/xamarin/ios/internals/limitations for more information.
Regression?
Yes. This is a regression from .NET 10.0.15.
Known Workarounds
Deserialize without supplying JsonSerializationContext. Deserialization will still work in WASM AOT mode.
var model = JsonSerializer.Deserialize<Foo<DateTimeOffset>>(json);Configuration
.NET SDK Version: 11.0.100
OS: Windows 11 26300.9818
Architecture: x64
Browser: Microsoft Edge Dev 159.0.4393.0
Other information
No response
Activity
dotnet-policy-service commentedon Nov 16, 2026
Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Nov 16, 2026
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.
eiriktsarpalis commentedon Nov 16, 2026
For context, STJ switched to using unsafe accessors when writing to init-only properties in .NET 11, so it appears we're hitting a runtime limitation when handling them.
pavelsavara commentedon Nov 16, 2026
Is this possibly IL trimming problem ?
eiriktsarpalis commentedon Nov 16, 2026
If that was case, wouldn't this also repro in Native AOT?
pavelsavara commentedon Nov 16, 2026
Reproduced on
release/11.0at2c1aa9e5a750using the in-tree browser sample with Mono, Release configuration, and AOT publishing. Blazor is not required. The same source passes without AOT.The root cause is inconsistent identity/cache handling for generic
UnsafeAccessorwrappers in Mono, rather than IL trimming:DateTimeOffsetsetter wrapper.compile_specialpath instead callsmono_marshal_get_unsafe_accessor_wrapperdirectly on the inflated accessor. This can create another wrapper definition in the instantiated method's wrapper cache.MonoMethod*identity. Instrumented execution showed identical requested/decoded signatures but different wrapper definitions, so the existing AOT code was not found. The eventual "Attempting to JIT compile" exception is a consequence of that lookup failure.As a diagnostic experiment, routing
compile_specialthrough the existingmini_replace_generated_methodpath made the requested and decoded wrapper identities match, and the unchanged AOT sample passed in Chrome. Restoring the original runtime reproduced the failure again.I will add a targeted runtime regression test and fix the inconsistent wrapper construction.
Note
This comment was prepared with GitHub Copilot.