[blazor][wasm] optimize async JS interop via async JSImport/JSExport#65912
Closed
pavelsavara wants to merge 2 commits intodotnet:mainfrom
Closed
[blazor][wasm] optimize async JS interop via async JSImport/JSExport#65912pavelsavara wants to merge 2 commits intodotnet:mainfrom
pavelsavara wants to merge 2 commits intodotnet:mainfrom
Conversation
This was referenced Mar 23, 2026
Open
This was referenced Mar 28, 2026
Open
Open
Open
b33a38f to
8005323
Compare
This was referenced Mar 31, 2026
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.
Replace begin/end callback pattern with direct Promise↔Task JS interop on WebAssembly
Summary
This PR replaces the
BeginInvokeJS/EndInvokeJSandBeginInvokeDotNet/EndInvokeDotNetcallback-based async interop pattern on WebAssembly with direct Promise↔Task marshalling via JSImport/JSExport. The server and WebView paths are unchanged.Motivation
The existing async JS interop on WebAssembly uses a two-step callback pattern:
BeginInvokeJSdispatches a call with anasyncHandle, JS executes the function, then calls back into .NET viaEndInvokeJSwith the serialized result.BeginInvokeDotNetwith acallId, .NET executes the method, then calls back into JS viaendInvokeDotNetFromJSwith the result.Each async interop call requires two cross-boundary transitions. On WebAssembly, where JS and .NET share a single thread and JSImport/JSExport natively support
Task↔Promisemarshalling, this round-trip is unnecessary overhead.Changes
.NET→JS async calls (
InvokeAsync<T>)JSRuntime.InvokeJSAsync(in JSInvocationInfo)— a new virtual method that returnsTask<string?>directly. The default implementation returnsnull(meaning: fall back toBeginInvokeJS).WebAssemblyJSRuntimeoverridesInvokeJSAsyncto call a new[JSImport] InvokeJSJsonAsyncthat returns aTask<string?>marshalled from the JS Promise.JSRuntime.InvokeAsync<T>usesOperatingSystem.IsBrowser()(a JIT intrinsic compiled away on each platform) to select the direct path on WASM or the begin/end path elsewhere.invokeJSJsonAsynccallsprocessJSCall, awaits the result, serializes it, and returns it — noasyncHandlebookkeeping needed.JS→.NET async calls (
invokeMethodAsync)DotNetDispatcher.InvokeAsync— a new public method that invokes the target[JSInvokable]method and, if it returns a Task/ValueTask, awaits and serializes the result into aTask<string?>.[JSExport] InvokeDotNetAsynconDefaultWebAssemblyJSRuntimethat callsDotNetDispatcher.InvokeAsyncand returns theTask<string?>directly to JS as a Promise.invokeDotNetFromJSAsyncavailable,invokeDotNetMethodAsynccalls it directly and deserializes the resolved Promise value — noasyncCallId/endInvokeDotNetFromJSbookkeeping needed.BeginInvokeDotNet/endInvokeDotNetFromJSare no longer called on WebAssembly.beginInvokeDotNetFromJSin MonoPlatform throws if called (defensive guard).endInvokeJSFromDotNetis a no-op.Cleanup
asyncHandleparameter fromInvokeJSJson(sync path only).InternalCalls.EndInvokeDotNetFromJS— replaced byInvokeJSJsonAsync.WebAssemblyJSRuntime.EndInvokeDotNetis now a no-op (async JS→.NET results flow via Promise resolution).WebAssemblyJSRuntime.BeginInvokeJS(long, string, string, ...)throwsNotSupportedException(retained only because the base class declares it abstract).Performance wins
_pendingAsyncCallsmap in JS and_pendingTasksConcurrentDictionaryin .NET are no longer used on the WASM path. No allocation for the async handle, no dictionary insert/lookup/remove per call.TaskCompletionSource<T>orCancellationTokenRegistrationcreated inJSRuntimefor the WASM path — the JSImport marshaller provides theTaskdirectly.assemblyNameordotNetObjectIdinto a single string parameter (workaround for the 4-arg JSExport limit) and parsed it back withlong.Parse. The new path passes typed parameters directly.Public API changes