Skip to content

Throw clear error when invoking async JSInvokable method synchronously - #68789

Open
SergioAlmeida29 wants to merge 2 commits into
dotnet:mainfrom
SergioAlmeida29:fix/46811-jsinterop-sync-invoke-async
Open

Throw clear error when invoking async JSInvokable method synchronously#68789
SergioAlmeida29 wants to merge 2 commits into
dotnet:mainfrom
SergioAlmeida29:fix/46811-jsinterop-sync-invoke-async

Conversation

@SergioAlmeida29

Copy link
Copy Markdown
  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Summary of the changes (Less than 80 chars)

Throw clear error when invoking async JSInvokable methods synchronously

Description

Calling a Task- or ValueTask<T>-returning [JSInvokable] method via synchronous invokeMethod serialized the live task instance, producing confusing JSON serialization errors (or meaningless JSON for non-generic ValueTask). DotNetDispatcher.Invoke now detects these results and throws an InvalidOperationException naming the method and directing to invokeMethodAsync. The async dispatch path (BeginInvokeDotNet) is unaffected. Tests cover void / Task / Task<T> × sync / async invokers.

Fixes #46811

Calling an async Task- or ValueTask-returning [JSInvokable] method via
the synchronous invokeMethod path serialized the live task instance,
producing confusing JSON serialization errors, or silently returning
meaningless state for a non-generic ValueTask. DotNetDispatcher.Invoke
now detects these results and throws an InvalidOperationException naming
the method and directing to invokeMethodAsync. The asynchronous dispatch
path is unaffected.

Fixes dotnet#46811

Signed-off-by: Sergio Almeida <sergioalmeida29.05@gmail.com>
Copilot AI lite review requested due to automatic review settings August 25, 2026 23:38
@SergioAlmeida29
SergioAlmeida29 requested a review from a team as a code owner August 25, 2026 23:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Aug 25, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @SergioAlmeida29. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@SergioAlmeida29

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

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

Labels

community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling Asynchronous .NET JS Interop Method Synchronously from JS Produces Misleading Error Message

2 participants