SignalR Core: Hub Context from Static Controller Method

1.8k Views Asked by At

I'm looking for a way to call a method, from another class in my project, that will then send a message to all clients connected to the mainHub

I have a static method where I'm trying to send data to all clients connected to the mainHub which is a SignalR Core hub. I am able to get the hub's context however, since this method is static, that hub's context requires an object reference.

In regular SignalR, I was able to use

var context = GlobalHost.ConnectionManager.GetHubContext<mainHub>();

I could then call this context.Clients.All. However, this does not exist in SignalR Core.

public class utils : Controller
{
    private readonly IHubContext<mainHub> _hubContext;

    public utils(IHubContext<mainHub> hubContext)
    {
        _hubContext = hubContext;
    }

    public static void addManagementLog(sstructs.ManageLog mngLog)
    {
        _hubContext.Clients.All.SendAsync("newManageLog", mngLog);
    }
}

An object reference is required for the non-static field, method or property utils._hubContext

What's the best way to use this hub context in a static method, or in a way that I can call this addManagementLog() method from other classes in this project?

0

There are 0 best solutions below