3

I am trying to get the hubcontext from a SignalR hub.

I want to be able to send notifications to the clients connected to that hub, while the hub instance may be disposed of.

The caveat is that I register the hubs dynamically, so I have the hub type as a Type object.

It is registered that way:

Application.UseSignalR(R => MapperMethodInfo.MakeGenericMethod(H).Invoke(R, new object [] { Path }));

With H being the hub type.

I have tried:

    var HubContext = ServiceProvider.GetService(H);

it returns null.

and I have tried:

    Application.Use(async (Context, Next) =>
    {
        var HubContext = Context.RequestServices.GetRequiredService(H);
    });

The lambda expression is not called.

I have read: https://learn.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-2.2

and it is the one proposing the method above.

And I have also found this solution: Get Hub Context in SignalR Core from within another object

which can't work since I need to know the hubs at compile-time, which I don't since they come in the form of plug-ins, in external assemblies, and are found and registered with reflection.

All online posts and solutions revolve around the two methods above and don't work in this case.

How can I get the hub context then?

Fred
  • 3,365
  • 4
  • 36
  • 57
Thomas
  • 10,933
  • 14
  • 65
  • 136

0 Answers0