Skip to content

Fix misleading error for synchronous async JS interop calls - #69659

Open
shantanu2002git wants to merge 2 commits into
dotnet:mainfrom
shantanu2002git:fix/shantanu/46811-jsinterop-error
Open

shantanu2002git wants to merge 2 commits into
dotnet:mainfrom
shantanu2002git:fix/shantanu/46811-jsinterop-error

Conversation

@shantanu2002git

@shantanu2002git shantanu2002git commented Oct 4, 2026 •

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.

Summary of the changes (Less than 80 chars)

Fix misleading error for synchronous async JS interop calls

Description

Fixes #46811.

When a [JSInvokable] method returning Task, Task<T>, ValueTask, or ValueTask<T> is invoked synchronously from JavaScript, the synchronous dispatcher previously attempted to serialize the returned asynchronous value, resulting in a misleading serialization error.

This change detects asynchronous return types before invoking the method and throws a clear InvalidOperationException directing callers to use invokeMethodAsync.

The existing asynchronous JS interop path is unchanged.

Changes

  • Detect Task and ValueTask return types in the synchronous dispatcher.
  • Throw a clear error before invoking the asynchronous method.
  • Add regression tests for generic and non-generic Task and ValueTask return types.

Testing

  • DotNetDispatcherTest: 67 passed, 0 failed
  • Microsoft.JSInterop.Tests: 174 passed, 0 failed

Fixes #46811

@shantanu2002git
shantanu2002git requested a review from a team as a code owner October 4, 2026 17:22
@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label Oct 4, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 4, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@shantanu2002git

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

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 review overview

🟡 Changes recommended

The regression test does not verify that the asynchronous target method remains uninvoked.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Improves synchronous JS interop diagnostics when targeting asynchronous .NET methods.

Changes:

  • Detects Task and ValueTask return types before invocation.
  • Adds regression coverage for generic and non-generic variants.
File Description
DotNetDispatcher.cs Rejects synchronous calls to asynchronous methods.
DotNetDispatcherTest.cs Tests the new diagnostic across supported async types.

Comment on lines +115 to +116
var exception = Assert.Throws<InvalidOperationException>(() =>
DotNetDispatcher.Invoke(jsRuntime, new DotNetInvocationInfo(null, methodIdentifier, targetReference.ObjectId, default), argsJson));

@shantanu2002git shantanu2002git Oct 8, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Resolved @PureWeen

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 15, 2026
@azure-pipelines

Copy link
Copy Markdown
Commenter does not have sufficient privileges for PR 69659 in repo dotnet/aspnetcore

@kotlarmilos kotlarmilos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks!

This branch has not been deployed

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

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

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

3 participants