Blazor.NET: What causes "A task was canceled" within JavaScript

Viewed 4890

I've got a Server Side Blazor.NET application (dotnet core 3.1) with library: ProtectedBrowserStorage (https://www.nuget.org/packages/Microsoft.AspNetCore.ProtectedBrowserStorage) for encrypted storage in Client Browswer

Sometimes there are errors in my log files that looks like:

2020-04-01 14:05:17.4809 Error System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at Microsoft.JSInterop.JSRuntime.InvokeWithDefaultCancellation[T](String identifier, Object[] args)
   at Microsoft.AspNetCore.ProtectedBrowserStorage.ProtectedBrowserStorage.GetAsync[T](String purpose, String key)
   at PegasusV6.LocalStorageService.LoadBasket(MenuDTO menu) in LocalStorageService.cs:line 40 A task was canceled. 

My problem is that I don't know what causes this error in JavaScript calls.

Is there maybe a Server restriction that can fix this kind of error?

Or is it client side that user maybe has not good internet speed / connection which is highly required for server side Blazor?

Hope someone can give me a hint or anything what could improve it. This is the C# function that is called:

    public async Task LoadBasket(MenuDTO menu)
    {
        try
        {
            AppState.BasketData = await ProtectedLocalStore.GetAsync<BasketState>($"Basket_{My.StoreId}") ?? new BasketState();
        }
        catch (Exception ex)
        {
            My.Log.Error(ex);
            AppState.BasketData = new BasketState();
        }
    }

Thanks!

2 Answers

There's a default maximum to the size of an incoming hub message. The first time I encountered this issue I tried to change the ApplicationMaxBufferSize and TransportMaxBufferSize of the blazor hub but it didn't solve the issue.

Instead add the SignalR service and set the MaximumReceiveMessageSize.

        services.AddSignalR(e => {
            e.MaximumReceiveMessageSize = 1000;
        });

https://docs.microsoft.com/en-gb/aspnet/core/signalr/configuration?view=aspnetcore-3.0&tabs=dotnet#configure-server-options

Hope this helps.

I got that error sometimes on JsInterop calls to javascript methods without a return value. I learned that it's best to always return a value even if it would not be necessary for the business logic.

That would look something like this in the blazor page:

await JSRuntime.InvokeAsync<bool>("exampleJsMethod");

and then in JS:

window.exampleJsMethod = () => {
    //do something
    return true;
}
Related