• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1602
  • Last Modified:

How do I call SetSessionStateBehavior(SessionStateBehavior.ReadOnly) in ASP .Net MVC SignalR 2.x application?


I'm relatively new to the world of SignalR. I've got a couple of questions some of ya'll can possibly help me with.

My question sets are two fold. I've been looking at a chat example and there are a couple of things I'm trying understand.

This sample is built using SignalR 1.x libraries.
The code sample from the Global.asax.c file is as follows:
using System;
using System.Web;
using System.Web.Routing;
using System.Web.SessionState;
namespace MyApplication
    public class Global : System.Web.HttpApplication
        private static RouteBase hubRoute;
        protected void Application_Start(object sender, EventArgs e)
            hubRoute = RouteTable.Routes.MapHubs();
        protected void Application_BeginRequest(object sender, EventArgs e)
            if (IsSignalRRequest(Context))
                // Turn readonly sessions on for SignalR
        private bool IsSignalRRequest(HttpContext context)
            RouteData routeData = hubRoute.GetRouteData(new HttpContextWrapper(context));
            // If the routeData isn't null then it's a SignalR request
            return routeData != null;

Open in new window

Question 1) I'm not clear on the concept of SetSessionStateBehavior being set to ReadOnly. What is the effect of this and why would we do this in the SignalR instance?

Question 2) I've been currently tasked with moving our implementation from the SignalR 1.x to 2.x libraries. How would I replicate this functionality using the 2.x libraries?

1 Solution

1- Session state is not supported from within SignalR as it interferes with the processing of simultaneous requests from the same user. In 2.0 HTTPContext.Current.Session property is null because SignalR requests are handled before the session state module is initialized.

2- You don't need to duplicate this behavior anymore as its the default and only behavior for SignalR

for more troubleshooting SignalR issues, see this
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now