-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add error message when invoking async methods synchronously in JSInterop #64273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add error message when invoking async methods synchronously in JSInterop #64273
Conversation
|
Thanks for your PR, @@ernest-koguc. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
|
@dotnet-policy-service agree |
|
ideally this would be roslyn analyzer, not runtime change |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds validation to prevent synchronous invocation of asynchronous JSInvokable methods. The change detects when an async method is called through the synchronous DotNetDispatcher.Invoke path and throws a helpful error message directing users to use BeginInvokeDotNet instead.
- Introduces async method detection using
AsyncStateMachineAttribute - Updates
InvokeSynchronouslyto accept anisAsyncContextparameter to distinguish between sync and async invocation paths - Adds validation that throws
InvalidOperationExceptionwhen attempting to invoke async methods synchronously
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| DotNetDispatcher.cs | Adds IsAsyncMethod helper and async/sync context validation to prevent misuse of async methods in synchronous invocation paths |
| DotNetDispatcherTest.cs | Adds test case CannotInvokeAsyncMethodSynchronously to verify the new validation behavior |
|
|
||
| Assert.Equal($"The method 'InvokableAsyncMethod' cannot be invoked synchronously because it is asynchronous. Use '{nameof(DotNetDispatcher.BeginInvokeDotNet)}' instead.", ex.Message); | ||
| } | ||
| internal class SomeInteralType |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected spelling of 'SomeInteralType' to 'SomeInternalType'.
| internal class SomeInteralType | |
| internal class SomeInternalType |
But then it would be a change in dotnet/razor repo right? Should I close this one then? |
Add error message when invoking async methods synchronously in JSInterop
Added error message indicating root cause issue when tyring to invoke async method synchronously through js interop.
Description
This pull request updates the
DotNetDispatcherin JSInterop to indicate root issue when asynchronous .NET methods are invoked synchronously from JavaScript. It introduces a runtime check that throws an exception if an async method is invoked synchronously. The changes also add a targeted unit test to verify this behavior.DotNetDispatcher.InvokeSynchronouslyto throw anInvalidOperationExceptionif an async method is invoked synchronously, guiding users to useBeginInvokeDotNetfor async methods.IsAsyncMethodusing theAsyncStateMachineAttributeto reliably detect async methods.isAsyncContextflag, distinguishing between sync and async invocation contexts. This is needed because InvokeSynchronously is used by both sync and async version. [1] [2] [3]System.Runtime.CompilerServicesimport to support async method detection.Added a unit test
CannotInvokeAsyncMethodSynchronouslyinDotNetDispatcherTest.csto verify that attempting to invoke an async method synchronously throws the correct exception and message.Fixes #46811