I am getting this exception in my Blazor Web App, whenever i try to use JsonSerializer while AOT is enabled. I get the exception when running in debug mode, which i find strange
System.InvalidOperationException: 'Reflection-based serialization has been disabled for this application. Either use the source generator APIs or explicitly configure the 'JsonSerializerOptions.TypeInfoResolver' property.'
Steps to reproduce my problem:
Create a Blazor Web App (WebAssembly) with .NET 8. Got to project properties and enable AOT
Goto Home.razor and add this code, so we trigger the use of System.Text.Json.JsonSerializer
@code{
protected override void OnInitialized()
{
var test = System.Text.Json.JsonSerializer.Serialize("Hello World");
}
}
Now run the project in debug mode. When it tries to serialize, i get this exception
I managed to solve the issue. When I used the Blazor Web App Wasm template, it created two projects for me. A normal project, and a .Client project. Do not enable AOT for the normal project. Enable AOT for the .Client project
Go to project properties for the .Client project, and enable AOT
When I publish my project now, everything works. My published code is 2.5x faster, than a non-AOT publish.
Like H H said, according to Microsoft documentation, native AOT is not supported for Blazor. Maybe there is a difference between "AOT" and "native AOT"?