0

We are using the Azure SignalR service from functions to send messages back to our UI and all is working without issue. But I can't find a definitive answer on how long lived the ServiceManager or HubContext should be.

At the moment each time we want to send a message to the UI we call a class we have written which does the following:

using var serviceManager = return new ServiceManagerBuilder().WithOptions(option =>
                {
                    option.ConnectionString = _connectionString;
                    option.ServiceTransportType = ServiceTransportType.Persistent;
                })
                .WithNewtonsoftJson()
                .BuildServiceManager();


await using var hubContext = await serviceManager .CreateHubContextAsync(hubName, System.Threading.CancellationToken.None);

await hubContext.Clients.Group(group).SendAsync(method, message);

This all works fine, but we are creating a new ServiceManager and ServiceHubContext every time we send a message.

The samples I have looked at do not include running in functions where we inject a service which handles publishing. Should either of these be Singleton? The functions we have are processing data and sending updates in a loop so we may send 100s of messages in a single function.

user351711
  • 3,171
  • 5
  • 39
  • 74
  • Have the same question. I'm also trying to understand whether creation of new HubContext instance will result in creation of the new connection between client (Azure Function) and SignalR service – Siarhei Machel May 23 '22 at 14:23
  • 2
    I asked on the repo and they responded: https://github.com/Azure/azure-signalr/issues/1578, HubContext is safe to be singleton. – user351711 May 24 '22 at 11:55

0 Answers0