我最近在我的 ASP.NET MVC3 应用程序中将 SignalR 从 0.4.0 更新到 0.5.2。在 SignalR 0.5.2 中,当用户断开连接时,Context 实例上的“User”属性为 null
- 这是设计使然吗?
从下面的“即时窗口”快照可以看出,当客户端断开连接时,我所知道的关于 Context
的信息就是断开连接的客户端的 ConnectionId。
Context
{SignalR.Hubs.HubCallerContext}
ConnectionId: "... some connection id ..."
Headers: null
QueryString: null
RequestCookies: null
ServerVariables: null
User: null
简化后,我的 SignalR Hub
实现如下所示:
public class MyHub : Hub, IDisconnect, IConnected
{
// some other code
public Task Disconnect()
{
var user = Context.User; // <-- This is null
// more code
}
}
总而言之 - 我是不是在这里忘记了什么,还是 Context.User 是 null
的设计?如果是这样 - 为什么? :)
最佳答案
我在 SignalR wiki 中找到了答案。在其有关集线器的部分 ( https://github.com/SignalR/SignalR/wiki/Hubs ) 中指出:
Whenever a client disconnects, the Disconnect method will be invoked on all hubs that implement IDisconnect. When this method is called you can use Context.ConnectionId to access the client that disconnected. NOTE: This method is called from the server, that means state on the Caller object, any state that was with the connection, as well as the HubContext's User and Cookies will not be populated.
关于asp.net-mvc-3 - SignalR 0.5.2 - Context.User 在断开连接时为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11529866/