How to get SignalR Hub Context in a ASP.NET Core?

user2095880 picture user2095880 · Dec 4, 2014 · Viewed 30.1k times · Source

I'm trying to get the context for a hub using the following:

var hubContext = GlobalHost.ConnectionManager.GetHubContext<SomeHub>();

The problem is that GlobalHost is not defined. I see it is part of the SignalR.Core dll. At the moment, I have the following in my project .json file, under dependencies:

"Microsoft.AspNet.SignalR.Server": "3.0.0-*"

If I add the latest available version of Core:

"Microsoft.AspNet.SignalR.Server": "3.0.0-*",
"Microsoft.AspNet.SignalR.Core" :  "2.1.2"

I get a whole bunch of errors because server and core are conflicting. If I change them to both use version "3.0.0-*", all the conflicts go away, but GlobalHost cannot be found. If I remove Server, and just user Core version 2.1.2 then GlobalHost works, but all the other things needing Server, obviously do not.

Any ideas?

Answer

Soren picture Soren · Sep 20, 2017

IConnectionManager does not exist any more in SignalR for ASP.Net Core.
I've been using HubContext for getting access to a hub.

public class HomeController : Controller
{
    private readonly IHubContext<LiveHub> _hubContext;

    public HomeController(IHubContext<LiveHub> hubContext)
    {
        _hubContext = hubContext;
    }

    public void SendToAll(string message)
    {
        _hubContext.Clients.All.InvokeAsync("Send", message);
    }
}

I'm using .net core 2.0.0 and SignalR 1.0.0-alpha1-final