How do I access the HttpContext of the "default connection" when in the SignalR Persistentconnection environment

Sven Leuschner 1 Reputation point
2022-09-14T14:52:33.273+00:00

Hello,
I found a nice book describing signalR. As we have a somewhat related task at hand (send multiple notifications to specific clients) I found signalR a good fit for the task at hand.
My problem comes from details as it is mostly the case.
I run a timer to poll a database table for new data to be send. Now in our database we have a user table and thus my table carries a userid field so that I can select data only meant for that specific user. I implemented a dictionary <int,string> to map the various userids to their signalR connectionIds so I can then pull the client's connectionId from said dictionary and

string client;  
if (!UserConnections.TryGetValue(UserInfo.UserID, out client))  
{  
    return;  
}  
this.Connection.Send(client, message);  

use this code section to send the message to its appropriate client.

Our application is generally a asp net mvc 4 application.
I looked at how controllers get the current user - they instantiate an object that pulls the user's data from the current session (cookie).

Now when I'm in the signalR persistent connection (or a child class thereof) any call to System.Web.HttpContext.Current yields null - which is now clear because the persistent connection doesnt know the default connection.

Instead I found hints to get the hubcontext but sadly at least for now
var requestContext = request.GetHttpContext();
string user = requestContext.User.Identity.Name;
shows "" - simply because I didnt set a name for the client yet. I also doubt that it will be filled with the login-name for the default connection

So is there ANY way to gain access to the properties of the "default connection" that the app built to the browser-window running this instance ?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,244 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 54,866 Reputation points
    2022-09-14T15:44:38.803+00:00

    for each signal/r connection there is state data until the connection is closed. this state data has the context information for the request that created the connection. the only direct access to this state data is via the client sending a message.

    if you want external access, then you need to define a shared storage and api that is maintained by the connections.in your case, you probably want to add the user name to the connection dictionary.

    0 comments No comments