1

I have this error when trying to send a message to SingalR after a trigger in an Azure Function. Next code https://learn.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-6.0 had to be adapted with the next problem (Clients would be null): SignalR Hubs Clients are null when firing event

Anyways, code of the Hub:

    public class NotificationHub : Hub
    {
        private IHubContext<NotificationHub> _context;

        public NotificationHub(IHubContext<NotificationHub> context)
        {
            _context = context;
        }
        
        public override async Task OnConnectedAsync()
        {
            var companyIds = Context.User?.GetCompanyIds();
            if (companyIds != null)
            {
                await companyIds.ForEachAsync(async companyId => await Groups.AddToGroupAsync(Context.ConnectionId, companyId));
            }

            await base.OnConnectedAsync();
        }

        public async Task SendMessageToGroup(int companyId, string message)
        {
            await _context.Clients.Group(companyId.ToString()).SendAsync("ReceiveMessage", "SignalR", message);
        }
    }

When calling SendMessageToGroup, I get "Azure SignalR Service is not connected yet, please try again later.".

Partial Startup:

        public static void Main()
        {
            var host = new HostBuilder()
                .ConfigureFunctionsWorkerDefaults()
                .ConfigureServices(s =>
                {
                    s.AddSignalR().AddAzureSignalR(SignalRConnectionString);
                    s.AddSingleton<NotificationHub>();
                })
                .Build();
 
            host.Run();
        }

OnConnectedAsync is never called. This is dotnet 5. Service mode of SingalR is Default.

Wouter
  • 154
  • 17

0 Answers0