'SignalR Hub throws exception accessing Group

Here's my code

public interface IJobClient
{
    Task DisplayMessage(string message);
}

public class JobHub : Hub<IJobClient>
{
    public const string GROUP_NAME = "JobGroup";

    private readonly ILogger<JobHub> logger;

    public JobHub(ILogger<JobHub> logger)
    {
        this.logger = logger;
    }

    public override async Task OnConnectedAsync()
    {
        await Groups.AddToGroupAsync(Context.ConnectionId, GROUP_NAME);

        try
        {
            await Clients.Group(GROUP_NAME).DisplayMessage($"{Context.ConnectionId} has joined the group {GROUP_NAME}.");
        }
        catch (Exception e)
        {
            logger.LogError("OnConnectedAsync raised exception {0} with message {1} at {2}", e.GetType(), e.Message, e.StackTrace);
        }

        await base.OnConnectedAsync();
    }

    public override async Task OnDisconnectedAsync(Exception exception)
    {
        await Groups.RemoveFromGroupAsync(Context.ConnectionId, GROUP_NAME);
        await base.OnDisconnectedAsync(exception);
    }

    public async Task SendGroupMessage(string message)
    {
        try
        {
            await Clients.Group(GROUP_NAME).DisplayMessage(message);
        }
        catch (Exception e)
        {
            logger.LogError("SendGroupMessage raised exception {0} with message {1} at {2}", e.GetType(), e.Message, e.StackTrace);
        }
    }
}

The client connects OK. It hits Groups.AddToGroupAsync() and that works fine, on the very next line it tries to send a message to the group it's just added a connection to, and blows up with a Null exception.

Can anyone help?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source