StefanFFM
StefanFFM

Reputation: 1898

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

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.

Upvotes: 4

Views: 2459

Answers (2)

adaraz
adaraz

Reputation: 179

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

Upvotes: 3

Rami A.
Rami A.

Reputation: 10582

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

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

Upvotes: 0

Related Questions