Get SignalR User (Hub.Context) outside of hub

本小妞迷上赌 提交于 2019-12-12 13:45:27

问题


is there any way to get current signalR request user outside the hub? I can use Hub.Context.User inside of hub methods, but if hub method calls any other underlying layer? Wcf service call - an additional BehaviorExtensionElement is used to add wcf message header with current user identity name.

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
     request.Headers.Add(MessageHeader.CreateHeader(
        Constants.HeaderNames.SessionId, 
        Constants.HeaderNames.HeaderNamespace,
       _hubManager.ResolveHub(Constants.Hubs.MessengerHub).
                   Context.User.Identity.Name));
}

Yes, i found that the DefaultHubManager gets the Hub, but i'm not sure it will be the hub from current request, not the concurrent one or a new one, 'cause at the end of ResolveHub i see the following code runs

   return (DependencyResolverExtensions.Resolve(this._resolver, descriptor.HubType) 
       ??  Activator.CreateInstance(descriptor.HubType)) as IHub;

Obviuosly i can pass user from hub method to wcf call but it requires refactoring to move from wcf behaviour to setting request field with user name explicitly. Also can i rely on HttpContext.Current to get the info from cookie?


回答1:


No you cannot. The only way to retrieve the currently active user outside of the hub itself would be to pass the users information to whatever method you call in the hub.

A common pattern is to track your users by adding them to some sort of dictionary in OnConnected and removing them in OnDisconnected. You can then have an entirely separate way of identifying your users while having required information that's associated with them.



来源:https://stackoverflow.com/questions/20001990/get-signalr-user-hub-context-outside-of-hub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!