6

I have a Blazor server app and every couple of minutes I get the following in my logs:

Error 12:09:54 [] Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager - Navigation failed when changing the location to https://louishowe-dev.azurewebsites.net/Identity/Account/Login TaskCanceledException: A task was canceled.
   at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args)
   at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, Object[] args)
   at Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager.<>c__DisplayClass13_0.<<NavigateToCore>g__PerformNavigationAsync|0>d.MoveNext()
Error 12:09:54 [] Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost - Unhandled exception in circuit 'GdWjUPhwt9bdljJjyKo0M-eVaVKtacUkhqCtNuwOg-s'. TaskCanceledException: A task was canceled.
   at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args)
   at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, Object[] args)
   at Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager.<>c__DisplayClass13_0.<<NavigateToCore>g__PerformNavigationAsync|0>d.MoveNext()

The program continues to run fine and the user sees no issue. But exceptions are generally bad news. What's going on here and how do I avoid this?

3 Answers 3

2

This is a known issue in ASP.NET Core, but it seems that it's not solved yet - although it is marked as solved. You could find the existing issue here: aspnetcore Github issue

Sign up to request clarification or add additional context in comments.

Comments

1

I found a solution for this after searching for two days in another post here: https://github.com/dotnet/aspnetcore/issues/45267

I changed my code in RedirectToLogin.Razor to:

@inject NavigationManager UriHelper
@inject IJSRuntime JSRuntime

@code {
    [Parameter]
    public bool IsAuthenticated { get; set; }

    protected override void OnInitialized()
    {
        if (!IsAuthenticated)
        {
            var redirectUrl = UriHelper.ToBaseRelativePath(UriHelper.Uri);
            if (!string.IsNullOrEmpty(redirectUrl))
            {
                //UriHelper.NavigateTo($"Login?redirectUrl={Uri.EscapeDataString(redirectUrl)}", true);
                JSRuntime.InvokeVoidAsync("window.location.assign", $"/login?returnUrl={redirectUrl}");
            }
            else
            {
                //UriHelper.NavigateTo("Login", true);
                JSRuntime.InvokeVoidAsync("window.location.assign", $"/login");
            }
        }
        else
        {
            UriHelper.NavigateTo("Unauthorized", true);
        }
    }
}

@inject Microsoft.Extensions.Localization.IStringLocalizer<RedirectToLogin> L

In words: I replaced NavigationManager.NavigateTo by JSRuntime.InvokeVoidAsync("window.location.assign", $"/login");

Since then I had no more exceptions.

Working with .net 8.0.11

Comments

0

From Microsoft support:

  1. Increase the SignalR message size and disable the JSRuntime call timeouts, as these could be possible causes of the exception. You can find more details on how to do this in the attached KB article.

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

  1. Make sure that the JavaScript methods you call via JSRuntime always return a value, and check your network connection for any interruptions. These could also affect the JSRuntime calls. Here are some forums where other developers have discussed similar problems.

(BlazorServer) each navigation to external logs 2 errors a minute later · Issue #45267 · dotnet/aspnetcore · GitHub

Trouble with NavigateTo within OnInitialized · Issue #13582 · dotnet/aspnetcore · GitHub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.