SignalR doesn't use Session on server
Asked Answered
S

3

32

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?

Saucedo answered 21/10, 2011 at 20:1 Comment(0)
B
47

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.

Bismarck answered 21/10, 2011 at 20:11 Comment(7)
I understand. So i think I will need call some ajax to register somewhere on the server who subscribed on the Hub.Saucedo
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...Plio
What would be a good alternative to store data to if DB should be avoided and this data should be available in Hub?Funest
WOW... I got in the SessionState trap when used it with SignalR: https://mcmap.net/q/454866/-signalr-serversentevents-on-windows-server-2008-with-iis-7-post-request-taking-too-much-time-to-complete/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! :)Snigger
@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?Funest
@Osi: No. Other requests that do use session don't affect requests (like SignalR requests) that don't.Bismarck
remember any alternative to persistant storage needs to survive if IIS AppPool or IIS is recycled - so be sure to accommodate that scenarioAraucania
R
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.

Roughspoken answered 4/5, 2015 at 9:8 Comment(0)
W
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()
Wolfy answered 8/7, 2016 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.