Reputation: 34830
The SignalR wiki covers how to broadcast over a hub from outside of a hub. However, this calls the client side operation:
var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.Group(group).addMessage(message);
Is there a way to invoke the Hub Operation, ideally in a strongly typed way? e.g.
GetHubContext<MyHub>().Invoke(h => h.Say(message))
I know this is possible by connecting to the hub using the .NET client but this seems wrong for when the calling code is on the same server as the hub.
Upvotes: 3
Views: 2629
Reputation: 41579
Bit late to the party but, yes, there is.
The stockticker sample nuget package does this (although it doesn't entirely use it!):
Basically, you expose a static instance of the server side hub to make calls against.
In the sample the pattern is:
public class StockTicker
{
// Singleton instance
private readonly static Lazy<StockTicker> _instance = new Lazy<StockTicker>(
() => new StockTicker(GlobalHost.ConnectionManager.GetHubContext<StockTickerHub>().Clients));
...
public static StockTicker Instance
{
get
{
return _instance.Value;
}
}
...
This is then available to be called from anywhere and pretty much anywhen!:
StockTicker.Instance.OpenMarket();
The same example is also covered by a blog post at the asp.net site
Upvotes: 5
Reputation: 526
Take a look: https://github.com/i-e-b/SignalR-TypeSafeClient
you can use this library.
Tiz
Upvotes: 0
Reputation: 38864
Nope. That's like trying to invoke an MVC controller from the server side (you just don't do it). Just move the common logic into a shared helper and call that helper from both the hub and your other server side code.
Upvotes: 1