我想分享ServiceStack OOB ICacheClient和SignalR集线器之间的高速缓存中的元素,但我发现了以下错误,当我尝试在OnDisconnected事件,以获取用户会话共享ServiceStack ICacheClient与SignalR
只有ASP.NET请求通过访问单身支持
我没有任何问题,在OnConnected事件访问会话,到目前为止,这是我做了什么:
public class HubService:Hub
{
private readonly IUserRepository _userRepository;
private readonly ICacheClient _cacheClient;
public HubService(IUserRepository userRepository,ICacheClient cacheClient)
{
_userRepository = userRepository;
_cacheClient = cacheClient;
}
public override System.Threading.Tasks.Task OnConnected()
{
var session = _cacheClient.SessionAs<AuthUserSession>();
//Some Code, but No error here
return base.OnConnected();
}
public override System.Threading.Tasks.Task OnDisconnected()
{
var session = _cacheClient.SessionAs<AuthUserSession>();
return base.OnDisconnected();
}
}
我使用的是简单的注射器和我ICacheClient被注册为单:
Container.RegisterSingle<ICacheClient>(()=>new MemoryCacheClient());
的问题是我怎么注册请求在SS单身?我在SignalR事件上错过了什么?
编辑:
什么我试图expain为注册请求在SS是因为如果有使用容器注册SS IHttpRequest并设置生活方式单的可能性,由于异常消息,它好像HttpContext的和IHttprequest都为空由OnDisconnected事件
的SS代码如下:
public static string GetSessionId(IHttpRequest httpReq = null)
{
if (httpReq == null && HttpContext.Current == null)
throw new NotImplementedException(OnlyAspNet); //message
httpReq = httpReq ?? HttpContext.Current.Request.ToRequest();
return httpReq.GetSessionId();
}
我想要做的是存储一个使用ICacheClient的连接用户列表,我只想在用户断开连接时从列表中删除connectionID。
编辑: 似乎根据danludwig post
像“有一个关于SignalR一件有趣的事情......当来自 集线器的客户端断开连接(例如,通过关闭浏览器窗口),它将创建一个 Hub类的新实例,以便调用OnDisconnected() 发生这种情况时,HttpContext.Current为null,因此如果此Hub具有任何>注册每个Web请求的依赖项,出错。”
以上的完美匹配我的情况
我不认为简单注射器的注册与其他任何容器相比有很大不同,所以如果任何人都可以为不同的容器回答这个问题,请做。我将用Simple Injector详细信息(和upvote)更新该答案。不幸的是,我目前无法回答这个问题,因为我不知道“注册请求”意味着什么。 – Steven