Click here to Skip to main content
15,843,200 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I working on blazor application server side . i face error when apply session timeout
Category: Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager
EventId: 4
SpanId: 7336f0e333159b9e
TraceId: 1c34008822a09f63877a1646dd4c58c1
ParentId: 0000000000000000
RequestId: 80000121-0000-f300-b63f-84710c7967bb
RequestPath: /_blazor
TransportConnectionId: XIZLCkB1z0H-hsteYUeJJQ

Navigation failed when changing the location to /Dashboard/Meid

Exception: 
System.Threading.Tasks.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 happen on Dashboard page on InvokeAsync method on dashboard page but why this issue happen this is only my question


my code details Dashboard Page

What I have tried:

code details
Java
protected override void OnInitialized()
    {
        
        DateTime now = DateTime.Now;
        string nowString = now.ToString("yyyy-MM-ddTHH:mm:ss");
        JS.InvokeVoidAsync("localStorage.setItem", "LastActivity",
       now);
    }

function checkSessionTimeout(currentUrl) {
    var sessionTimeout = 20 * 60 * 1000; // 20 minutes in milliseconds
    var lastActivity = new Date(Date.parse(localStorage.getItem("LastActivity"))); // get the last activity time from the client-side local storage
    console.log("current date" + Date());
    console.log("last date store" + lastActivity);
    if (new Date() - lastActivity > sessionTimeout) {
        localStorage.clear(); // clear the local storage
        window.location.href = "/Login/Login"; // redirect to login page
    } else {
        setTimeout(function () { checkSessionTimeout(currentUrl); }, 1000); // check again in 1 second
    }
}

checkSessionTimeout(window.location.href);
Posted
Updated 1-May-23 2:55am
v3

1 solution

Here is your issue:
C#
 protected override void OnInitialized()
{
    DateTime now = DateTime.Now;
    string nowString = now.ToString("yyyy-MM-ddTHH:mm:ss");
    JS.InvokeVoidAsync("localStorage.setItem", "LastActivity", now);
}

You are using "fire and forget" with the JS.InvokeVoidAsync method. This is a big no-no unless you know what you are doing.

Change to:
C#
protected override async Task OnInitializedAsync()
{
    DateTime now = DateTime.Now;
    string nowString = now.ToString("yyyy-MM-ddTHH:mm:ss");
    await JS.InvokeVoidAsync("localStorage.setItem", "LastActivity", now);
}

You can learn more about the lifecycles here: Blazor University - Component lifecycles[^]
 
Share this answer
 
Comments
ahmed_sa 1-May-23 2:58am    
JS.InvokeVoidAsync not working on event OnInitializedAsync()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900