8

I'm wondering if it's possible to Cast the result of var hub = GlobalHost.ConnectionManager.GetHubContext<ChatHub>(); To my actual ChatHub class. Because GlobalHost.ConnectionManager.GetHubContext<ChatHub>() as ChatHub fails

On my ChatHub class I have a method UpdateTime():

public void SendTimeUpdate(DateTime time, string auth)
{
    Clients.All.UpdateTime(time, auth);
}

And I want to call it from my other class. Since I can't cast to ChatHub and invoke the SendUpdate I have to go:

GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients.All.UpdateTime(time, auth);

But if I go this road, the method SendTimeUpdate isn't added in the proxy script /signalr/hubs

Is there a solution for this problem? I want to get the typed Hub instance and not call stuff directly on the Clients property of the IHubContext.

user1613512
  • 3,590
  • 8
  • 28
  • 32
  • So the method `UpdateTime` needs to be defined in your client code. Could you post whatever javascript you have? – mrtig Jan 11 '14 at 22:26

2 Answers2

2

No you cannot cast the result of ....GetHubContext<.... to your hub class. Sorry :(.

The GetHubContext approach returns an IHubContext when a Hub is only an IHub.

If you'd like to centralize the logic just make a method that you can call into from your hub and from your external service.

N. Taylor Mullen
  • 18,061
  • 6
  • 49
  • 72
1

Couldn't your class just create a connection to your hub and call the method that way?

Gjohn
  • 1,261
  • 1
  • 8
  • 12