简体   繁体   English

与SignalR类(集线器)共享静态类

[英]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. 当用户登录时,用户的信息已保存到HttpContext会话中,这是一个好主意。

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.. 现在,我添加了一个signalR集线器类(稍后将添加多个),并且我了解到我们无法访问HttpContext会话,因此我无法取回用户的信息。

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. 每个会话都有一个ID,我们可以从HttpContext会话和Hub Context中获得该ID。

  1. From HttpContext.Current.Session, it's SessionID 在HttpContext.Current.Session中,它是SessionID
  2. From a Hub class, it's Context.Request.Cookies["ASP.NET_SessionId"] 在Hub类中,它是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. 当用户登录并浏览网站时,静态类的效果很好,但是在Hub类(signalR服务器)中,静态类为空。这就像该类的新“实例”一样,这对我来说很奇怪。

Notes 笔记

All my classes are in App_Code, even the Hub class. 我所有的类都在App_Code中,甚至在Hub类中。 All the classes are in COMPILE mode except the Hub class (when set to COMPILE, the server doesn't start) which stays as CONTENT. 除保留为CONTENT的Hub类(设置为COMPILE时,服务器无法启动)外,所有这些类均处于COMPILE模式。

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. 不知何故,从App_Code文件夹中删除了Hub类,从而解决了该问题。 Now I can access the static class with no problem. 现在,我可以毫无问题地访问静态类了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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