Throw meaningful exception when async JSInvokable method is called synchronously from JS#65880
Open
iamcymentho wants to merge 1 commit intodotnet:mainfrom
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves the developer experience for JS interop by detecting when an async [JSInvokable] method is invoked via the synchronous JS path (invokeMethod()), and throwing a clear InvalidOperationException instead of letting Task/ValueTask be serialized into misleading JSON-related errors.
Changes:
- Add sync-path detection for
Task,ValueTask, andValueTask<T>return values inDotNetDispatcher.Invoke(), throwing a targeted error message. - Add new unit tests covering sync invocation of
Task-returning andValueTask-returning[JSInvokable]methods. - Add a small test helper
[JSInvokable]method that returnsTask.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/JSInterop/Microsoft.JSInterop/src/Infrastructure/DotNetDispatcher.cs | Adds sync-path check for async return types and throws a clearer exception before serialization. |
| src/JSInterop/Microsoft.JSInterop/test/Infrastructure/DotNetDispatcherTest.cs | Adds tests to validate the new exception behavior for Task and ValueTask sync invocation. |
src/JSInterop/Microsoft.JSInterop/src/Infrastructure/DotNetDispatcher.cs
Show resolved
Hide resolved
src/JSInterop/Microsoft.JSInterop/test/Infrastructure/DotNetDispatcherTest.cs
Show resolved
Hide resolved
src/JSInterop/Microsoft.JSInterop/test/Infrastructure/DotNetDispatcherTest.cs
Show resolved
Hide resolved
src/JSInterop/Microsoft.JSInterop/test/Infrastructure/DotNetDispatcherTest.cs
Show resolved
Hide resolved
bfacf3d to
f28aff8
Compare
…nchronously from JS When a [JSInvokable] method that returns Task or ValueTask is invoked synchronously from JavaScript via invokeMethod(), the raw Task/ValueTask object gets serialized, producing misleading JSON errors like "SerializeTypeInstanceNotSupported, System.Action". This makes debugging difficult as the error is unrelated to the actual issue. Add a check in DotNetDispatcher.Invoke() to detect async return types (Task, ValueTask, ValueTask<T>) and throw an InvalidOperationException with a clear message guiding the developer to use invokeMethodAsync(). Fixes dotnet#46811
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #46811
When a
[JSInvokable]method that returnsTaskorValueTaskis invoked synchronously from JavaScript viainvokeMethod(), the raw Task/ValueTask object gets serialized, producing misleading JSON errors like:"SerializeTypeInstanceNotSupported, System.Action""ConstructorContainsNullParameterNames, System.Threading.Tasks.Task'1"These errors make debugging difficult as they are unrelated to the actual problem.
Changes
DotNetDispatcher.cs- In theInvoke()method (sync code path), added a check afterInvokeSynchronously()returns to detect async return types (Task,ValueTask,ValueTask<T>). When detected, throws anInvalidOperationExceptionwith a clear message:DotNetDispatcherTest.cs- Added 2 tests and 1 test helper:SyncInvokeOfAsyncMethod_Task_ThrowsMeaningfulExceptionSyncInvokeOfAsyncMethod_ValueTask_ThrowsMeaningfulExceptionInvokableAsyncReturningTask()- simple[JSInvokable]test methodTest results
All 148 JSInterop tests pass with 0 failures.
Test plan
SyncInvokeOfAsyncMethod_Task- verifies Task-returning methods throw clear errorSyncInvokeOfAsyncMethod_ValueTask- verifies ValueTask-returning methods throw clear error