-
Notifications
You must be signed in to change notification settings - Fork 25.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Access to IHubContext<MyHub> from the background thread/task #8925
Comments
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. |
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? |
Provided that you have access to the DI container. The best solution I've found:
|
@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, 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" |
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.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: