Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is HTTPContext.Current.Session null using SignalR 2.x libraries in a ASP .Net MVC application?

I'm attempting to migrate our ASP.Net MVC application from using Signal R1.x tgo SignalR 2.x. I just found issue which will certainly cause us problems in our quest to move forward.

Our web application is MVC based and makes heavy use of the HttpContext.Current.Session variable. When running with SignalR 1.x, everything is fine and dandy with Session.

When we upgraded to SignalR 2.x, Session was suddenly null.

I did a little googling and found the following links regarding the issue: HTTPContext.Current.Session is nul in SignalR...need alternate to Session state while using SignalR

Further investigation revealed the following tidbit of information: http://www.asp.net/signalr/overview/signalr-20/troubleshooting-and-debugging/troubleshooting

HTTPContext.Current.Session is null
This behavior is by design. SignalR does not support the ASP.NET session state, 
since enabling the session state would break duplex messaging.

I would imagine that this will break a great many ASP .Net MVC based applications. Is there any workaround for this?

Is this truly by design, or is it a result of using oWin::>Startup::Configuration(){app.MapSignalR();} to initiate signalR in 2.x?

If possible, I’d like to figure this out and if the solution is not so painful, they will still consider moving to 2.x.

Thoughts?

like image 295
JohnB Avatar asked Feb 25 '14 18:02

JohnB


People also ask

What is a SignalR error?

This error may be seen if the project contains a folder called SignalR, which will interfere with the automatically-created proxy. To avoid this error, do not use a folder called SignalR in your application, or turn automatic proxy generation off.

What is SignalR in ASP NET MVC?

ASP.NET SignalR is a library for ASP.NET developers to add real-time web functionality to their applications. Real-time web functionality is the ability to have server-side code push content to the connected clients as it happens, in real-time.

What is long polling in SignalR?

SignalR is a Microsoft framework specifically designed to facilitate real-time client/server communication. It provides an effective implementation of long polling that doesn't have a deep impact on the server, and at the same time ensures that clients are appropriately updated about what's going on remotely.


1 Answers

Session state is not supported from within SignalR as it interferes with the processing of simultaneous requests from the same user, and is not supported from WebSockets requests. In 2.0 the property is null because SignalR requests are handled before the session state module is initialized. This was a change from 1.0 in how SignalR is hosted inside of System.Web based ASP.NET applications (in 1.0 it used a route in the ASP.NET routes table, in 2.0 it uses the OWIN hosting module provided by Microsoft.Owin.Host.SystemWeb).

Note this only affects the use of session from within your SignalR classes (Hubs, etc.), not the rest of your application.

like image 76
Damian Edwards Avatar answered Oct 02 '22 21:10

Damian Edwards