0

I am trying to use SignalR, which I am fairly sure I have right. What I am doing is trying to fire off a server side method at an interval using SignalR client -> server calls. In the server side method I have a condition that the Session["User"] must not be null, which it cannot possibly be null at the time this method fires because it fires from a control once that control is loaded. However, I am getting a null exception when the method is called on this condition none the less.

here is the C# server side code:

[HubName("timer")]
public class Timer : Hub
{
    public void Time()
    {
        QueryFactory qFactory = new QueryFactory();

        if (HttpContext.Current.Session["User"] != null)
        {
            var ui = (UserInfo)HttpContext.Current.Session["User"];
            var userId = ui.UserID;
            var userName = ui.Name;
            var serverName = Dns.GetHostName();

            if (!userName.Equals(""))
            {
                var query = qFactory.GetQuery("P_UPDATE_CURRENT_USER");
                query["userid_in"] = Int32.Parse(userId);
                query["last_changed_in"] = DateTime.Now;
                query["server_name_in"] = serverName;
                query.Execute();
            }
        }
    }
}

here is the client side code that is calling the server side:

$(function () {
    var hub = $.connection.timer;
       if (<%=Session["User"]%> !== null) {
           alert(<%=Session["User"]%>);
           $.connection.hub.start().done(function() {
               setInterval(function() {
                   hub.server.time();
               }, <%=ConfigurationManager.AppSettings["timerInterval"]%>);
           });
       }
});

notice that in the client side script I put an alert out to fire BEFORE the server side method is called and this alert returns a session user value so I know the session is valid before this call happens. Any ideas why this null error is happening?

John
  • 505
  • 1
  • 9
  • 22
  • In my opinion, `SignalR` and `Session` are diametric opposites and shouldn't be used together. `Session` is _per_ user - the point is to hold different data for each. `SignalR` is about pushing the _same_ data to multiple users. – Oded Jan 25 '13 at 20:55
  • While your point is true that is only one use for SignalR. I am using it as a way to detect when a browser is no longer open and has not been closed in a predictable manner. If the browser stops calling the server side method, a timestamp in my database is not updated. This timestamp field in the database is watched by a job running on the database that will wipe a user out of a table thus ending a lockout. I know this is a crappy thing to do but I am bound by what my client wants so before anyone lectures me on the do's and don't I understand but there is nothing I can do. – John Jan 26 '13 at 00:53

2 Answers2

1

SignalR now has read-only access to the session. If you do not want to do it that way you can access the session in the client and use a querystring to pass the information to the hub.

Client side Code:

var uid = <%=Session["User"]%>;
$.connection.hub.qs = "uid=" + window.encodeURIComponent(uid)

Server side Code:

var userId = Context.QueryString["uid"];
John
  • 505
  • 1
  • 9
  • 22
0

You can put a breakpoint in your code to see where the exception is coming from (system.diagnostics.debugger.launch()). Just by looking at your code, the only thing that stands out is var query = qFactory.GetQuery("P_UPDATE_CURRENT_USER"); Could query be null?

TYY
  • 2,702
  • 1
  • 13
  • 14
  • well technically yes but the code never gets to that point. It is giving me a null error at the if statement – John Jan 26 '13 at 00:54