public override Task OnDisconnected(bool stopCalled) { CurrentlyUsedWebPage userTrace = _repo.GetAllWebPages().SingleOrDefault(x => x.ConnectionId == new Guid(Context.ConnectionId)); // try to delete if user has not reconnected Task mytask = Task.Run(() => { UserDisconnected(userTrace, _repo, Context.ConnectionId); }); return(base.OnDisconnected(stopCalled)); }
private async void UserDisconnected(CurrentlyUsedWebPage userTrace, CurrentlyUsedWebPageRepository repo, string connectionId) { // copy userTrace to local var, this gets disposed by CLR after some time. var localUserTrace = userTrace; await Task.Delay(_userTimeToLive); // make sure localtrace isn't null if (localUserTrace == null) { return; } // check db if a connection still exist bool hasUserReconnected = repo.GetAllWebPages().Any(x => x.User == localUserTrace.User && x.Url == localUserTrace.Url && x.ConnectionId != new Guid(connectionId)); // delete if disconnected if (!hasUserReconnected) { repo.DeletePage(connectionId); Update(userTrace, 4); } }