JSRuntime.InvokeAsync("open", ...) throws TaskCanceledException

Viewed 698

In my blazor-server-side app I am calling JSRuntime.InvokeAsync to open static content in a popup window:

await _jsRuntime.InvokeAsync<object>("open", "/help/help.html", "_blank");

It works, but after some time (probably a timeout?) a TaskCanceledException is thrown. I tried calling InvokeVoidAsync, but the effect is the same. I can fix this by catching and ignoring the exception or by removing "await", but I was hoping for a cleaner solution that does not give me complier warnings.

2 Answers

Try specify cancellationToken as CancellationToken.None

await _jsRuntime.InvokeAsync<object>("open", System.Threading.CancellationToken.None, "/help/help.html", "_blank");

A cancellation token to signal the cancellation of the operation. Specifying this parameter will override any default cancellations such as due to timeouts (DefaultAsyncTimeout) from being applied.

Documenation here

To resolve the compiler error, use discard '_':

_ = _jsRuntime.InvokeAsync<object>("open", "/help/help.html", "_blank");
Related