when implement session time out i get error on invoke sync method ?

Posted 1 year ago by ahmedsa
0

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

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);

this error happen on some remote pc but local pc this issue not happen

also when connect to server remote from my pc it not happen

some remote pc connected to this page happen this issue .

  • 0

    Try changing your OnInitialized to this and see if it helps:

    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);
    }
    Posted 1 year ago by selliott
  • 0

    it working on same pc and another pc not working

    Posted 1 year ago by ahmedsa
  • 0

    I'm not sure why it would work on one machine, but not the other. However, if you're prerendering your Blazor Server app, you may need to invoke your JavaScript methods after rendering has completed.

    Try moving that call to the OnAfterRenderAsync method:

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
    	if (firstRender)
    	{
        	DateTime now = DateTime.Now;
        	await JS.InvokeVoidAsync("localStorage.setItem", "LastActivity", now);
        }
    }    
    Posted 1 year ago by selliott Edited 1 year ago
Someone is typing...

Post a Reply

You must be logged in to add a new post.
Number of online users: 1
An error has occurred. This application may no longer respond until reloaded. Reload 🗙