Skip to content

System.Text.Json deserialization fails for certain init-only struct fields in Blazor WASM AOT mode #137604

Open

Description

@DL444

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

  1. Create a new Standalone Blazor WebAssembly application from template targeting .NET 11
  2. Enable WASM AOT publishing by adding <RunAOTCompilation>true</RunAOTCompilation> property to the project file
  3. Replace the content of Pages/Home.razor with 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;
}
  1. 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.
  2. Serve the published website in wwwroot with dotnet serve or other similar tools
  3. 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_01<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

  1. added
    untriaged
    on Nov 16, 2026
  2. dotnet-policy-service commented on Nov 16, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-json
    See info in area-owners.md if you want to be subscribed.

  3. dotnet-policy-service commented on Nov 16, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
    See info in area-owners.md if you want to be subscribed.

  4. eiriktsarpalis commented on Nov 16, 2026

    @eiriktsarpalis
    Member

    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.

  5. pavelsavara commented on Nov 16, 2026

    @pavelsavara
    Member

    Is this possibly IL trimming problem ?

  6. eiriktsarpalis commented on Nov 16, 2026

    @eiriktsarpalis
    Member

    If that was case, wouldn't this also repro in Native AOT?

  7. self-assigned this
    on Nov 16, 2026
  8. added this to the 11.0.0 milestone on Nov 16, 2026
  9. added
    os-browser
    and removed
    untriaged
    on Nov 16, 2026
  10. pavelsavara commented on Nov 16, 2026

    @pavelsavara
    Member

    Reproduced on release/11.0 at 2c1aa9e5a750 using 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 UnsafeAccessor wrappers in Mono, rather than IL trimming:

    • The emitted AOT bitcode already contains the concrete DateTimeOffset setter wrapper.
    • AOT method replacement and runtime AOT-reference decoding obtain the wrapper for the generic accessor definition, then inflate that wrapper.
    • The runtime compile_special path instead calls mono_marshal_get_unsafe_accessor_wrapper directly on the inflated accessor. This can create another wrapper definition in the instantiated method's wrapper cache.
    • Extra-method lookup compares 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_special through the existing mini_replace_generated_method path 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.

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

Metadata

Metadata

Assignees

Labels

arch-wasmarea-Codegen-AOT-monoos-browser

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions