asp.net-mvc-3 - SignalR 0.5.2 - Context.User 在断开连接时为空

标签 asp.net-mvc-3 signalr

我最近在我的 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/

相关文章:

asp.net-mvc-3 - MVC3 : How can I route non-SSL requests automatically to the SSL protocol?

asp.net-mvc-3 - MVC 3 的 SignalR 实现

asp.net - SignalR 异步操作错误

architecture - 将用于微服务的 SignalR 与 API 网关集成

android - SignalR 与 android(Java) 有问题

asp.net - SignalR 2.0 超时连接

asp.net-mvc-3 - 提交后的mvc3复选框值

asp.net-mvc - 当没有数据时如何隐藏 MVC View 中的表?

asp.net-mvc-3 - 将 MVC3 数据库移至远程服务器。无法再删除。

c# - 如何实现RazorViewEngine FindView缓存