How to access IHubContext<MyHub> from the background thread/task that is not a part of a middleware pipeline?
In the previous version of SignalR there was a simple global object GlobalHost.ConnectionManager with a method GetConnectionContext.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@anurse please respond. @tdykstra should this go in the differences table?
See #8915 too
https://docs.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-2.1 already covers this in detail.
That is a link to this page, and its still not clear how to access the HubContext from a background thread/task. For instance injected HubContext seems to work well inside a controller, but if you resolve it in a class outside the asp.net request pipeline, and call hub.Clients.All.SendAsync() it doesnt throw error but doesnt work either. Please help.
but if you resolve it in a class outside the asp.net request pipeline, and call hub.Clients.All.SendAsync() it doesnt throw error but doesnt work either. Please help.
It should work fine as long as you get it through the DI container. Can you provide an example of the code that isn't working?
@anurse we need to do this doc https://github.com/aspnet/Docs/issues/8476.
Provided that you have access to the DI container. The best solution I've found:
public class Startup
{
...
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseMvc();
app.UseSignalR(route =>
{
route.MapHub<MyHub>("/MyHub");
});
mMyHubContext = app.ApplicationServices.GetService<IHubContext<MyHub>>();
}
public static IHubContext<MyHub> mMyHubContext;
}
@Jarek300 See the solution in the issue I linked. You can use an IHostedService to do this https://github.com/davidfowl/UT3/blob/fb12e182d42d2a5a902c1979ea0e91b66fe60607/UTT/Scavenger.cs#L9-L40.
Thanks @davidfowl and @anurse, using the proper IHostedService to host background tasks works perfectly. Oh and did I say? BackgroundService is great!
Reviewed the article, which has a good bit of information on not only how to inject a controller, but how to use the hub from within the controller. If one wanted to use the hub from a class that was used by the controller (so, a -> b -> c, where b is the controller and c is the class the controller is using, just inject the hub into the class via the same constructor injection used with the controller.
Closing this now as I feel it is a well-documented area already, but please re-open and mention me in your comments if you feel we need to go deeper on this topic. Thanks for your feedback!
@bradygaster we still need to do #8476
Yes I understand this (and hadn’t forgotten the thread). Anything specific you wanna show off in the doc/segment? Have aspirations for an entire doc on the topic or would you feel an inclusion in another doc to be “enough?"
Adding it to the existing doc would be fine. I would use searchable terms though like "background thread/task"
Most helpful comment
Provided that you have access to the DI container. The best solution I've found: