I'm trying to debug a SignalR hub and noticed that the constructor is getting called multiple times, even with a single client. Is this the expected behaviour? I was expecting the constructor to be called only once for class initialisation, but I'm hitting my breakpoint multiple times.
Asked
Active
Viewed 7,970 times
22
-
1not sure, but i think its a new one on every request – Mahmoud Darwish Jul 28 '13 at 23:48
2 Answers
24
In SignalR Hub instance will be created per each request. So it does't matter if there is only one client or more. In fact Hub is an abstraction over PersistentConnection, if you want more precise control over the things happening behind the scene you can use PersistentConnection. Check here : https://github.com/SignalR/SignalR/wiki/PersistentConnection

Incognito
- 16,567
- 9
- 52
- 74
-
3Even more than that. Once per invocation which can be more than per request (in the case of websockets) – davidfowl Feb 22 '14 at 09:38
10
As Incognito correctly pointed out, SignalR creates a new instance of the Hub for every request. You should use static members to store information you want in the Hub for all the requests. If you wish to do some operations every time a new client connects to the hub, you should put that code in the OnConnected.

Abhishek Nanda
- 1,007
- 1
- 8
- 14