I have a Signalr Hub called NotificationHub that handles sending new notification to coected 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 { retu _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 { retu _updateNotifier; }
set { _updateNotifier = value; }
}
public static NotificationManager GetInstance(Action<ActivityStream> dispatcher)
{
retu _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?
