简体   繁体   中英

Sharing a static class with SignalR classes (Hubs)

I am creating a website with a login form. When the user logs in, the user's informations were saved to the HttpContext session, it was a good idea.

Now I added a signalR hub class (I will be adding multiple later), and I understood that we cannot access the HttpContext session, so I had no way to get the user's informations back..

I thought maybe I could handle the sessions by myself, with a static class. Each session has an Id, and we can get that Id from both the HttpContext session and a Hub Context.

  1. From HttpContext.Current.Session, it's SessionID
  2. From a Hub class, it's Context.Request.Cookies["ASP.NET_SessionId"]

Now the idea was, to store the sessions with a dictionnary for each one representing the data.

Problem:

The static class works great when the user logs in and navigate on the website, but in the Hub class (signalR server), the static class is empty.. It's like a new "instance" of that class, which seems weird to me.

Notes

All my classes are in App_Code, even the Hub class. All the classes are in COMPILE mode except the Hub class (when set to COMPILE, the server doesn't start) which stays as CONTENT.

Anyone could clarify this to me? Maybe Im doing something wrong, or its the compile/content that ruins the idea. Thank you !

Somehow, moving out the Hub class from the App_Code folder fixed the issue. Now I can access the static class with no problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM