Skip to content
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

Closed
Jarek300 opened this issue Oct 9, 2018 — with docs.microsoft.com · 12 comments
Closed

Access to IHubContext<MyHub> from the background thread/task #8925

Jarek300 opened this issue Oct 9, 2018 — with docs.microsoft.com · 12 comments
Assignees
Labels
Pri1 High priority, do before Pri2 and Pri3
Milestone

Comments

Copy link

Jarek300 commented Oct 9, 2018

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.

@Rick-Anderson
Copy link
Contributor

Rick-Anderson commented Oct 11, 2018

@anurse please respond. @tdykstra should this go in the differences table?
See #8915 too

@Rick-Anderson Rick-Anderson added this to the Backlog milestone Oct 11, 2018
@analogrelay
Copy link
Contributor

Copy link

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.

@analogrelay
Copy link
Contributor

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?

@davidfowl
Copy link
Member

@anurse we need to do this doc #8476.

Copy link
Author

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;
}

@davidfowl
Copy link
Member

@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.

@ksgopal4
Copy link

Thanks @davidfowl and @anurse, using the proper IHostedService to host background tasks works perfectly. Oh and did I say? BackgroundService is great!

@Rick-Anderson Rick-Anderson added the Pri1 High priority, do before Pri2 and Pri3 label Oct 18, 2018
@bradygaster
Copy link
Member

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!

@davidfowl
Copy link
Member

@bradygaster we still need to do #8476

@bradygaster
Copy link
Member

bradygaster commented Nov 15, 2018 via email

@davidfowl
Copy link
Member

davidfowl commented Nov 16, 2018

Adding it to the existing doc would be fine. I would use searchable terms though like "background thread/task"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pri1 High priority, do before Pri2 and Pri3
Projects
None yet
Development

No branches or pull requests

6 participants