Search code examples
c#signalrasp.net-core-signalrazure-signalr

Should I create a new instance of IServiceHubContext each time (with CreateHubContextAsync) or remember and reuse?


I connection to Azure SignalR Service using Managmenet API. I use C# library for that and run this code:

var context = await _serviceManager.CreateHubContextAsync(hub);

What I failed to figure out is what is the cost of this operation and if there is some pooling behind the scenes. I.e.: should I remember and reuse the context (and it's resilient against errors) or should I create a new hub context for each operation?


Solution

  • According to https://github.com/Azure/azure-signalr/issues/1578:

    You could dispose ServiceManager once you have created the HubContext. And the HubContext is safe to use as a singleton across multiple threads.

    Having a look at the code, it looks like the ServiceManager creates its own IHost to maintain the lifetime of the objects