How Can the `id` Query String Parameter in a Blazor WebSocket Connection Be Accessed?

Viewed 119

When debugging with my Blazor server-side application, I see the following message:

[2022-02-25T10:40:19.177Z] Information: WebSocket connected to wss://<host>/_blazor?id=<_identifier_>.

I am interested in the id query string parameter specifically. Is this value available in any way within a Blazor application? I looked in the ComponentHub.Context.ConnectionId but it does not appear to match this value.

I do realize that I can use the IHttpContextAccesssor, but want to stay away from this as this is considered poor practice with Blazor applications.

(If I am mistaken about this particular scenario that would be great to know as well.)

1 Answers

This is an internal implementation detail of the SignalR handshake, and it is absolutely not meant for the developer to do anything with it.

For instance, if a websocket connection can't be established, because of networking issues, SignalR falls back to long polling, and each long polling request will get a new ID query parameter, which internally all map to the same SignalR connection id.

See also this great article why this is a bad idea: https://consultwithgriff.com/signalr-connection-ids/

Related