3

I'm having an odd issue that I cannot seem to figure out or even understand properly.

Basically I have a basic Authorisation method that fetches a security token (cookie) and compares it to the currently logged on user. When firing this from a SignalR method it throws a null exception but I know the user has security token. This works fine without SignalR.

I'll provide code etc..To help understand the problem.

The Architecture:

  1. Business Layer
  2. Presentation Layer

//Located within the business layer
public static User getUserBySecurityToken()
{
   string securityToken = "";
   if (HttpContext.Current.Request.Cookies["SecurityToken"] != null)
   {
     securityToken = HttpContext.Current.Request.Cookies["SecurityToken"].Value.ToString();                                                   
   } 

   return user;
}

//Located within the presentation layer (hubs)
public void Arrange(int tid, string status, string content) 
{
     //Get logged on user
    Business.User user = Business.User.getUserBySecurityToken();
    if (user != null)
    {
        Clients.Group(user.campaignName).changeTicket(tid, status, content);
    }

}

//Located within the presentation layer (client side script)
$(document).on('click', '.move-option-js', function (e) {
    //This does not even get called due to cookie being null
    window.hubReady.done(function ()
    {
        panelHub.server.arrange(i, s, content);
    });
});

Please note I have omitted some code for the readability. I hope I could get a better understanding of this problem and maybe slightly re-factor the code to get it working.

EDITS

After more testing all is fine and works perfect the first time but thereafter it fails.

Tez Wingfield
  • 2,129
  • 5
  • 26
  • 46
  • There is no HTTP requests during SignalR calls assuming WebSocket communication (or any WebSocket calls) - so it is reasonable you get null ref exception... – Alexei Levenkov Jan 01 '15 at 07:46
  • 1
    Thanks for your comment. I have added an "edit" to the Q. Basically works fine when firing the first and then fails after. In regards to HTTP well I suppose that makes. More digging! Do you have any suggestions on re-factoring? It's a shame considering my app is almost complete haha. Thanks again. – Tez Wingfield Jan 01 '15 at 09:04
  • http://stackoverflow.com/questions/12472940/signalr-and-httpcontext-session – Alexei Levenkov Jan 01 '15 at 09:41
  • Thanks a million! I managed to fix the problem but i don't really understand the problem need to investigate more. Not sure how i would give you a positive vote (thumbs up)? – Tez Wingfield Jan 01 '15 at 20:08
  • Thanks is enough :) . It would be nice if you write up self-answer that solved you problem and accept it (for parts that get your code working but you don't understand feel free to ask new questions). – Alexei Levenkov Jan 02 '15 at 01:11

1 Answers1

0

To the best of my knowledge the issue was SingalR running in it's own thread and my SecurityToken Method runs in the main thread. So after the first page load the main thread no longer exists, So you have to program within the SignalR Context.

I fixed the issue by overloading my original method and passing in a SignalR Context Cookie Request. Code Below:

The works from a class that inherits from the Hub.

Context.RequestCookies["SecurityToken"].Value.ToString()

Hope this helps future readers.

Regards,

Tez Wingfield
  • 2,129
  • 5
  • 26
  • 46