I have a Signalr Hub called NotificationHub that handles sending a new notification to connected clients. The NotificationHub class uses the NotificationManager class to retrieve notification data. Now I want to be able to use the session to store the last call to the new notification, but when using HttpContext.Current.Session ["lastRun"] in the NotificationManager, I get a NullReferenceException. To clarify, here are some of the codes for 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)
{
var notificationList = _notificationService.GetLatestNotifications();
_dispatcher(BuilActivityStream(notificationList));
}
}
I want to save the lastRun value for the session so that I can receive the next arrival of a new notification. How can i achieve this?
Edit:
, , , - , (-) . , lastRun, lastRun DateTime.Now. : , () , . , , .