I have a Signalr Hub called NotificationHub that handles sending new notification to connected clients. The NotificationHub class uses a NotificationManager class to retrieve notifications data. Now, I want to be able to use a session to store the last time a new notification has been accessed but when using HttpContext.Current.Session["lastRun"] in NotificationManager I get a NullReferenceException. To clarify more, here is some of the codes of both classes:
NotificationHub
[HubName("notification")]
public class NotificationHub : Hub
{
private NotificationManager _manager;
private ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public NotificationManager Manager
{
get { return _manager; }
set { _manager = value; }
}
public NotificationHub()
{
_manager = NotificationManager.GetInstance(PushLatestNotifications);
}
public void PushLatestNotifications(ActivityStream stream)
{
logger.Info($"Adding {stream.TotalItems} notifications ");
Clients.Caller.addLatestNotifications(stream);
}
//.....
}
NotificationManager
public class NotificationManager
{
private static NotificationManager _manager;
private DateTime _lastRun;
private DbUpdateNotifier _updateNotifier;
private readonly INotificationService _notificationService;
private readonly Action<ActivityStream> _dispatcher;
private long _userId;
private IUnitOfWork unitOfWork;
public NotificationService NotificationService => (NotificationService)_notificationService;
public DbUpdateNotifier UpdateNotifier
{
get { return _updateNotifier; }
set { _updateNotifier = value; }
}
public static NotificationManager GetInstance(Action<ActivityStream> dispatcher)
{
return _manager ?? new NotificationManager(dispatcher);
}
private NotificationManager(Action<ActivityStream> dispatcher)
{
_userId = HttpContext.Current.User.Identity.CurrentUserId();
_updateNotifier = new DbUpdateNotifier(_userId);
_updateNotifier.NewNotification += NewNotificationHandler;
unitOfWork = new UnitOfWork();
_notificationService = new NotificationService(_userId, unitOfWork);
_dispatcher = dispatcher;
}
private void NewNotificationHandler(object sender, SqlNotificationEventArgs evt)
{
//Want to store lastRun variable in a session here
var notificationList = _notificationService.GetLatestNotifications();
_dispatcher(BuilActivityStream(notificationList));
}
//....
}
I want to able to store the value of lastRun to a session to that I can retrieve the next time a new notification arrives. How can I achieve that?
Edit:
To clarify things up, what I want to store in session is the last time the server pushed new notification(s) to the client. I can use this value to only get notifications that happened after the current value of lastRun and then update lastRun to DateTime.Now. For example: Let's say a user has three new(unread) notifications and then two new notifications arrive. In this case the server has to know the time the last new notifications have pushed to the client so that it will only send those two new notifications.