Technologies:
Tolerim
a month ago
I receive an error stating that a task was cancelled when implementing session timeout in the InvokeVoidAsync function.
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
at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, Object[] args)
at Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager.<>cDisplayClass130.<<NavigateToCore>g_PerformNavigationAsync|0>d.MoveNext()
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);
Answers(2)
Vogi
a month ago
Verified Answer
Based on the error message you provided, it seems that the session timeout is causing the issue. The error message indicates that the task was canceled due to a session timeout. The code you provided shows that you are using local storage to store the last activity time and then checking it against the current time. If the difference between the two times is greater than 20 minutes, then you are clearing the local storage and redirecting to the login page.
It is possible that this error is happening only on some remote PCs because of network latency or other network-related issues. You may want to check if there are any network-related issues on those remote PCs.
You can also try increasing the timeout value to see if it resolves the issue. You can do this by adding the following code in your Startup.cs or Program.cs file:
app.UseEndpoints(endpoints =>
{
// other settings go here
endpoints.MapBlazorHub(options => options.WebSockets.CloseTimeout = new TimeSpan(1, 1, 1));
});
This will set the WebSocket close timeout to 1 hour. You can adjust this value as per your requirements.
I hope this helps! Let me know if you have any further questions.
Sources
c# - blazor Server timeout while debugging - Stack OverflowConnection disconnected with error 'Error: Server timeout ... - GithubSession Timeout redirect in Server side blazor - Stack OverflowHow Blazor manage user Session? - Stack OverflowTolerim
a month ago
It's difficult to determine the exact cause of the issue without more information, but it seems like the error is happening when the InvokeAsync method is called on the dashboard page.
Based on the error message, it looks like a TaskCanceledException is being thrown which might indicate that the task is timing out. It's possible that the remote server is taking longer to respond, causing the timeout to occur.
It's also worth noting that the checkSessionTimeout function is being called every second using the setTimeout method. This could potentially cause some performance issues on the client-side, especially if it's running on older or slower hardware.
As for why the issue is only happening on some remote PCs and not others, it's difficult to say without more information about the specific configurations of those machines. It's possible that there could be network issues or other circumstances that are causing the problem to occur on certain machines.