.GetAwaiter().GetResult() limitation in Blazor WebAssembly #57429
Labels
area-blazor
Includes: Blazor, Razor Components
✔️ Resolution: Answered
Resolved because the question asked by the original author has been answered.
Status: Resolved
Is there an existing issue for this?
Describe the bug
Hi,
Blazor WebAssembly seems to have some issues with the
Task.GetAwaiter().GetResult()
method. This method is typically used when we want to run an asynchronous task without the need of awaiting it. This results in asynchronous code running synchronously.However, when we try to use
Task.GetAwaiter().GetResult()
in Blazor WebAssembly, like in this code:we get an exception in the JS console:
While code in Task.Run(...) does seem to run, .GetAwaiter().GetResult() does result in an exception.
Expected Behavior
The expected behavior when we use .GetAwaiter().GetResult() is that the asynchronous code should run, albeit it might cause the page to hang for a bit depending on the amount of time it takes for that asynchronous code to complete (just like in Blazor Server or ASP.NETCore, or in desktop apps like Windows Forms or WPF).
Steps To Reproduce
Here is a simple, minimal code to reproduce the issue. Create a new Razor Component and paste this code:
BlazorFiddle
Exceptions (if any)
.NET Version
8.0.303
Anything else?
ASP.NETCore version: 8.0
IDE: I use both Visual Studio 2022 17.10.5 and VSCode, the issue persists.
dotnet --info
result (I had to translate some text):The text was updated successfully, but these errors were encountered: