Skip to content

[.NET 10 Preview 5] Blazor Web WebAssembly Mode crashes related with JsonSerialization #62350

@Kumima

Description

@Kumima

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I enable the System.Text.Json.Serialization.RespectRequiredConstructorParametersDefault as true for my projects. See: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/required-properties#feature-switch

It works fine before, but after updating to Preview5, I got:

AggregateException_ctor_DefaultMessage (An exception occurred executing JS interop: JsonRequiredPropertiesMissing, Microsoft.AspNetCore.Components.ResourceAsset, 'properties'. See InnerException for more details.)
    at un (marshal-to-js.ts:421:18)
    at rn.resolve_or_reject (marshal-to-js.ts:315:28)
    at marshal-to-js.ts:364:16
    at marshal-to-js.ts:341:48
    at gr (invoke-js.ts:528:9)
    at Mc (marshal-to-js.ts:341:5)
    at dotnet.native.ulywfn9r8g.wasm:0x1f60f
    at dotnet.native.ulywfn9r8g.wasm:0x1ca55
    at dotnet.native.ulywfn9r8g.wasm:0xeb73
    at dotnet.native.ulywfn9r8g.wasm:0x1f0f3

To disable RespectRequiredConstructorParametersDefault will fix, this seems a break change, and I want to keep my RespectRequiredConstructorParametersDefault as true.

My guess is Preview5 deserializes Microsoft.AspNetCore.Components.ResourceAsset without all required properties.

Describe the solution you'd like

With System.Text.Json.Serialization.RespectRequiredConstructorParametersDefault as true won't break my Blazor Web application. An approach is to make sure all required properties presented in the json string. Or:

public sealed class ResourceAsset(string url, IReadOnlyList<ResourceAssetProperty>? properties);
// Make the second param optional
public sealed class ResourceAsset(string url, IReadOnlyList<ResourceAssetProperty>? properties = null);

Additional context

My .NET SDK Version: 10.0.100-preview.5.25277.114
I'm using preview5 nuget packages for all my Blazor related packages.

Activity

javiercn

javiercn commented on Jun 16, 2025

@javiercn
Member

@Kumima thanks for contacting us.

This is not a breaking change on Blazor. I imagine we are not compatible with https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/required-properties because of the reason that you stated but might as well be that other data structures in addition to ResourceAsset are impacted by it.

If this behavior changed for you between preview4 and preview5, we would suggest you raise the issue on the runtime repo, since that might point to a false positive or it might be the case that a bug in the detection logic got fixed and is surfacing the error now.

Kumima

Kumima commented on Jun 16, 2025

@Kumima
Author

@javiercn Thanks for the reply.

I don't think a runtime repo issue will help. Since I still believe My guess is Preview5 adds some serialization of Microsoft.AspNetCore.Components.ResourceAsset without all required properties.

It is not a break change since by default RespectRequiredConstructorParametersDefault flag is false. The thing I was hoping is Blazor can improve the serialization stuffs to fit for the true value of RespectRequiredConstructorParametersDefault. Because this setting is more intuitive. If true value works, false value won't be affected.

This may not be a high priority task for you, but I still think this can be an improvement.

javiercn

javiercn commented on Jun 17, 2025

@javiercn
Member

Feature switch
You can turn on the RespectRequiredConstructorParameters setting globally using the System.Text.Json.Serialization.RespectRequiredConstructorParametersDefault feature switch. Add the following MSBuild item to your project file (for example, .csproj file):

<ItemGroup>
  <RuntimeHostConfigurationOption Include="System.Text.Json.Serialization.RespectRequiredConstructorParametersDefault" Value="true" />
</ItemGroup>

The RespectRequiredConstructorParametersDefault API was implemented as an opt-in flag in .NET 9 to avoid breaking existing applications. If you're writing a new application, it's highly recommended that you enable this flag in your code.

I think we want to turn this on in our E2E test projects as a first step.

  • We would need to add this to the csproj for components.testserver and other test projects (those that reference the Microsoft.NET.Sdk.BlazorWebassembly

Then we want to see what errors appear and then fix the issues.
There are two possible ways to three possible fixes

  • Add an additional constructor that doesn't include the missing properties. For example,
  1. public ResourceAsset(string url) : this(url, null)
  2. Add a default parameter value to the existing constructor ResourceAsset(string url, IReadOnlyList<ResourceAssetProperty>? properties = null);
  3. Change the type to use required properties instead of the constructor. public class ResourceAsset { public required string Url { get; init; } public IReadOnlyList<ResourceAssetProperty> Properties { get; init; } }

We can't make breaking changes, so 3 is only possible for internal types.

Kumima

Kumima commented on Jun 17, 2025

@Kumima
Author

There is another option which is
<RuntimeHostConfigurationOption Include="System.Text.Json.Serialization.RespectNullableAnnotationsDefault" Value="true" />, can they be handled together? Currently with this option on, I did not meet any problem.

added this to the 10.0-preview7 milestone on Jun 19, 2025
javiercn

javiercn commented on Jun 19, 2025

@javiercn
Member

@Kumima thanks for the additional details.

I think we would want this to work without having to enable additional JSON settings.
#62366 should cover the fix and updating our E2E tests to validate that things work with the more strict setting.

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

Metadata

Metadata

Assignees

Labels

area-blazorIncludes: Blazor, Razor Components

Type

No type

Projects

No projects

Relationships

None yet

    Participants

    @javiercn@Kumima

    Issue actions

      [.NET 10 Preview 5] Blazor Web WebAssembly Mode crashes related with JsonSerialization · Issue #62350 · dotnet/aspnetcore