Note that there are some explanatory texts on larger screens.

plurals
  1. POSharing ServiceStack ICacheClient with SignalR
    primarykey
    data
    text
    <p>I'm trying to share the elements in cache between ServiceStack OOB ICacheClient and a SignalR Hub, but I'm getting the following error when I try to get the user session in the OnDisconnected event</p> <blockquote> <p>Only ASP.NET Requests accessible via Singletons are supported</p> </blockquote> <p>I have no issues accessing the session in the OnConnected event, so far this is what I've done:</p> <pre><code>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&lt;AuthUserSession&gt;(); //Some Code, but No error here return base.OnConnected(); } public override System.Threading.Tasks.Task OnDisconnected() { var session = _cacheClient.SessionAs&lt;AuthUserSession&gt;(); return base.OnDisconnected(); } } </code></pre> <p>I'm using simple injector and my ICacheClient is registered as singleton:</p> <pre><code> Container.RegisterSingle&lt;ICacheClient&gt;(()=&gt;new MemoryCacheClient()); </code></pre> <p>the question is how do I register <strong>requests</strong> as singletons in SS? what am I missing on SignalR event?</p> <p><strong>Edit:</strong></p> <p>what I tried to expain for <em>register requests</em> in SS is because if there's a possibility to register SS IHttpRequest using a container and set the lifestyle as singleton due to the exception message, it seems like httpContext and IHttprequest are null by the OnDisconnected event</p> <p>the SS code is the following:</p> <pre><code>public static string GetSessionId(IHttpRequest httpReq = null) { if (httpReq == null &amp;&amp; HttpContext.Current == null) throw new NotImplementedException(OnlyAspNet); //message httpReq = httpReq ?? HttpContext.Current.Request.ToRequest(); return httpReq.GetSessionId(); } </code></pre> <p>what I'm trying to do is to store a list of connected users using ICacheClient and I just want to remove the connectionID from the list when a user get disconnected.</p> <p><strong>Edit:</strong> it seems like according to <a href="https://stackoverflow.com/questions/10555791/using-simpleinjector-with-signalr/18487801#18487801">danludwig post</a> </p> <blockquote> <p>"There is an interesting thing about SignalR... when a client disconnects from a hub (for example by closing their browser window), it will create a new instance of the Hub class in order to invoke OnDisconnected(). When this happens, HttpContext.Current is null. So if this Hub has any dependencies that are >registered per-web-request, something will probably go wrong."</p> </blockquote> <p>the description above perfectly match my situation </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload