Search code examples
asp.net-coresignalrsignalr-hubasp.net-core-signalr

SignalR OnConnected no suitable method found to overwrite?


My OnConnected is saying: 'NotificationHub.OnConnected()': no suitable method found to override

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;

namespace NotificationManager.Push.Hubs
{
    [Authorize]
    public class NotificationHub : Hub
    {
        public async Task SendNotificationAsync(List<string> users, string title, string message)
        {
            await Clients.Users(users).SendAsync("ReceiveNotification", title,  message);
        }

        public override Task OnConnected()
        {
            string name = Context.User.Identity.Name;

            Groups.AddToGroup(Context.ConnectionId, name);

            return base.OnConnected();
        }
    }
}

I have SignalR imported, I don't see how it can't find the method to override from the Hub extension.


Solution

  • That's because the method you want to override is OnConnectedAsync. You can read the docs at https://learn.microsoft.com/aspnet/core/signalr/hubs?view=aspnetcore-5.0#handle-events-for-a-connection for more info.

    Also, I suggest using an IDE for writing code, especially when you're not familiar with it, as that would make it easier to see what method you're trying to override.