SvenLeuschner-3690 avatar image
0 Votes"
SvenLeuschner-3690 asked LanHuang-MSFT commented

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

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))
 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 ?

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

now tested and indeed the User.Identity.Name is not filled with the typed in login-Name .. So I have the strange effect of the general website does see a logged in user, but my signalR connection still only knows the connectionId and nothing connecting that to an actual user

0 Votes 0 ·
LanHuang-MSFT avatar image LanHuang-MSFT SvenLeuschner-3690 ·

Hi @SvenLeuschner-3690,
SignalR provides a ConnectionId for each connection. To find out which connection belongs to whom (user), a mapping needs to be created between the connection and the user.
It depends on how you identify the user in your application.
For details, you can check the documentation: Mapping SignalR Users to Connections.
Best regards,
Lan Huang

0 Votes 0 · thx for your advice - but I expecitly ask for the "non-signalR" connection that serves the app outside the signalR stuff ...
Maybe I've mistranslated some things in my question, but initially I thought I could just add the signalR connection to my existing aspnetmvc app - without changing many things - and the first steps looked good, just add a startup.cs to set the mapping, include the nuget packages, write a piece of js code to handle the data coming from the signalR connection, write the server side code for the persistent connection .. done.
But going by our applications inner workings I have to have access to the httpcontext of the "general connection" - but within signalR I can only access the signalR httpcontext - so its not suitable for our case.
Done, stamped, sealed

0 Votes 0 ·
LanHuang-MSFT avatar image LanHuang-MSFT SvenLeuschner-3690 ·

Hi @SvenLeuschner-3690,

System.Web.HttpContext.Current yields null

I think you can change the method,In your SignalR Hub, use:

 System.Web.HttpContextBase httpContext = Context.Request.GetHttpContext();

Instead of HttpContext.Current.
Best regards,
Lan Huang

0 Votes 0 ·

1 Answer

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

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 your case, you probably want to add the user name to the connection dictionary.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.