31

When I try to access the HttpContext current session from the HUB it returns null.

I tried making use of the interface IRequiresSession but it didn't work. Can someone help me?

Markus
  • 3,225
  • 6
  • 35
  • 47
Danillo Corvalan
  • 703
  • 1
  • 10
  • 19

3 Answers3

46

SignalR connections (including the connection underlying all Hub operations for a client) do not support Session state. You could enable it if you wanted to but we'd strongly recommend against it as session state access serializes requests for a given client, meaning you won't really get the benefit from SignalR duplex messaging anymore, as one request will block the other e.g. in the long polling transport, the receiving connection will block any attempt to send.

Bob Horn
  • 33,387
  • 34
  • 113
  • 219
Damian Edwards
  • 7,289
  • 1
  • 25
  • 19
  • I understand. So i think I will need call some ajax to register somewhere on the server who subscribed on the Hub. – Danillo Corvalan Oct 21 '11 at 20:39
  • 3
    Thanks for the answer. As a work around, i read the AspNetSessionId cookie and looking up user information in a static list. Simulating a session... – nicojs Oct 19 '12 at 08:05
  • 2
    What would be a good alternative to store data to if DB should be avoided and this data should be available in Hub? – Zsolt May 03 '13 at 11:24
  • WOW... I got in the `SessionState` trap when used it with `SignalR`: http://stackoverflow.com/q/17768838/114029 Hey Damian: can you tell me if this recommendation of not using `SessionState` is included in SignalR docs? It'd be good to put it there in the forefront as a WARNING! :) – Leniel Maccaferri Jul 21 '13 at 10:38
  • @Damien Edwards: Does this also mean that a web application which uses sessions (e.g. for storing user related data after login), can not use signalr without this kind of latency? – Zsolt Dec 17 '13 at 14:42
  • @Osi: No. Other requests that do use session don't affect requests (like SignalR requests) that don't. – Damian Edwards Jan 03 '14 at 20:52
  • remember any alternative to persistant storage needs to survive if IIS AppPool or IIS is recycled - so be sure to accommodate that scenario – Simon_Weaver May 14 '15 at 00:26
1

you can simply maintain your own session, for example, use in memory cache, http is stateless,so server need session to maintain clients,but web socket is keep-alive connection, server keeps connected with clients, by default no need such things like HttpContext Session.

LIU YUE
  • 1,593
  • 11
  • 19
1

I resolve the same problem with hidden field as follows:

  1. Added ASP Hiddenfield on the aspx page.
  2. assigned the session value on page load (in my case on button client event)
  3. sent the value to the SignalR Hub using $("FieldID").val()
Pritam
  • 322
  • 2
  • 10