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.
What is a SignalR hub. The SignalR Hubs API enables you to call methods on connected clients from the server. In the server code, you define methods that are called by client. In the client code, you define methods that are called from the server.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With