I am using Microsoft.AspNetCore.SignalR
(latest release) and would like to get the hub context from within another object that's not a Controller
. In the "full" SignalR, I could use GlobalHost.ConnectionManager.GetHubContext<MyCoolHub>();
I have seen a lot of examples of just adding Microsoft.AspNetCore.SignalR.IHubContext<MyCoolHub>
as a parameter to the Ctor of a Controller
, but no examples (that work) for otherwise.
ETA:
So, this is what I have working. Is this hacky?
public class MyHub : Hub
public static IHubContext<MyHub> GlobalContext { get; private set; }
public MyHub(IHubContext<MyHub> ctx){
GlobalContext = ctx;
}
}
Then I can call it as so:
await MyHub.GlobalContext.Clients.All.InvokeAsync(...)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…