I am using SignalR 2 and I can not figure out how I can use my Hub methods e.g from inside a controller action.
I know I can do the following:
var hub = GlobalHost.ConnectionManager.GetHubContext<T>();
hub.Clients.All.clientSideMethod(param);
But that executes the method directly on the client side.
What if I have business logic inside my server side ClientSideMethod(param)
method I want to call from my controller the same way as when it is called from the client side?
At the moment I use public static void ClientSideMethod(param)
inside my hub and in that method I use the IHubContext
from the ConnectionManager
.
Is there no better was of doing this?
The following is not working (anymore in SignalR 2?):
var hubManager = new DefaultHubManager(GlobalHost.DependencyResolver);
instance = hubManager.ResolveHub(typeof(T).Name) as T;
instance.ClientSideMethod(param);
There I get a "Hub not created via Hub pipeline not supported" exception, when accessing the Clients.
It might work to create a "helper" class that implements your business rules and is called by both your Hub and your Controller:
As I did not find a "good solution" I am using @michael.rp's solution with some improvements:
I did create the following base class:
And then in the actual Hub (e.g.
public class AdminHub : Hub<AdminHub>
) I have (static) methods like the following: