无法通过 SignalR Hub 访问会话信息.我的设计有问题吗? [英] No access to the Session information through SignalR Hub. Is my design is wrong?

查看:23
本文介绍了无法通过 SignalR Hub 访问会话信息.我的设计有问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现您无法访问 SignalR 中心内的当前会话.

I've just discovered you can't access the current session within the SignalR Hub.

简化我的场景:我试着写一个聊天.

Simplified my scenario: I've tried to write a chat.

当前用户的名称保存在会话中.

The name of the current user was kept within the Session.

我使用 SignalR 更新(一组连接)关于每条新消息的信息.

I've used SignalR to update (a group of connections) about every new message.

现在我看到无法通过集线器访问当前用户的名称.

Now I see that I can't access the name of the current user through the hub.

我想可能有一些解决方法,但我的设计是错误的吗?

I guess there might be some workarounds, but is that implement my design was wrong?

我不应该为此目的使用 SignalR 吗?或者我不应该以这种方式使用 Session 吗?

Should I've not used SignalR for that purpose? Or should I not use Session in this way?

推荐答案

您不应将 Session 与 SignalR 一起使用(请参阅 SignalR 不使用服务器上的会话).您可以通过连接 ID 识别逻辑连接,您可以映射到用户名.

You shouldn't use Session with SignalR (see SignalR doesn't use Session on server). You identify logical connections by their connection id which you can map to user names.

根本问题在于对SessionState的访问在ASP.NET中是序列化的 以确保状态一致性,因此每个对集线器的请求都会阻塞其他请求.过去,通过设置 EnableSessionstate 为只读,这可以防止我描述的锁定问题)是可能的,但是 已放弃对此的支持.另请参阅各种其他地方 SignalR 团队在那里发表了类似的声明.最后:官方文档中有声明关于 HTTPContext.Current.Session.

The underlying problem is that access to SessionState is serialized in ASP.NET to ensure state consistency, so each request to the hub would block other requests. In the past, limited read-only access (I assume (but can't confirm since the gist is gone) by setting EnableSessionstate to read-only, which prevents the locking problem I described) was possible, but support for this was dropped. Also see various other places where the SignalR team made similar statements. Lastly: there's a statement in the official documentation about HTTPContext.Current.Session.

这篇关于无法通过 SignalR Hub 访问会话信息.我的设计有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆