In SignalR is Hub.Context thread safe?

谁说我不能喝 提交于 2019-12-01 00:28:57

问题


If there is more than one request occurring from different clients simultaneously then the value of Hub.Context.ConnectionId changes during the execution of the handler.

Say I've got 2 clients connected with client Ids A and B, and I've got a method on my Hub called foo(). I send a request from A to the Server invoking foo(), then whilst the request from A is being processed I send a request from B invoking foo(). At the start of the processing of A's request Hub.Context.ConnectionId == A but at the end of the method call Hub.Context.ConnectionId == B.

Should I be copying the Hub.Context? At what point should I do this?


回答1:


It doesn't need to be thread safe since Hub instances aren't static so you don't need to copy anything.

They are created per call. So each call from the client will create a new Hub instance and HubContext.




回答2:


This doesn't directly answer your question, but my usual workflow is to set a value on the caller when they first connect, which may accomplish what you need?

Caller.clientId = Guid.NewGuid();


来源:https://stackoverflow.com/questions/10180052/in-signalr-is-hub-context-thread-safe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!