Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

blazor

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!

like image 384
baer999 Avatar asked Apr 01 '20 19:04

baer999


People also ask

Can you use JavaScript in Blazor?

A Blazor app can invoke JavaScript (JS) functions from . NET methods and . NET methods from JS functions. These scenarios are called JavaScript interoperability (JS interop).

Is Blazor actually good?

Blazor is a tough sell to current web developer, because it means leaving behind many of the libraries and technologies that have been up over a decade of modern JavaScript development. It's not a seamless transition, and there's no way to migrate a JavaScript application to a Blazor project.

Is Blazor slower?

Performance. Blazor projects are slow on the client-side because you have to download the entire dot net runtime along with the necessary DLL libraries on your browser.

What is front end in Blazor?

With Blazor WebAssembly, you can write front-end code in one programming language – C#. That means developers just need to learn one programming language to create a dynamic web application. This single feature has multiple benefits, especially if you are running a Blazor project.


Video Answer


1 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.

like image 134
dev-in-towns-end Avatar answered Oct 03 '22 02:10

dev-in-towns-end