Reputation:
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?
Upvotes: 0
Views: 1904
Reputation:
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"];
Upvotes: 1
Reputation: 2716
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?
Upvotes: 0