1

I want to Call my Client methods from the controller/ServiceAssembly

Currently I am using

//Notify Others of the Login 
GlobalHost.ConnectionManager.GetHubContext<NotificationHub>().Clients.All.NotifyOthersAllOnLogin(string.Format("Recent Login({2}): {0} {1}", account.FirstName,account.LastName, account.LastLogin));

But I want to be able to inject an instance of hub in controller so that I can use Different Hub Methods.

I am using StructureMap V3for DependencyInjection.

Any Help/direction in this regard will be appreciated

ale
  • 10,012
  • 5
  • 40
  • 49
Ankesh
  • 4,847
  • 4
  • 38
  • 76

1 Answers1

3

There is a tutorial of dependency injection in SignalR: http://www.asp.net/signalr/overview/signalr-20/extensibility/dependency-injection. Example is for NInject, but you can easily customize it. The thing you need to remember, is to config your DI container before initializing the SignalR (mapping the hubs).

Then you can register your hub context to be able to resolve it. Very important thing is, that you register hub context after hubs are mapped. Hub context can be saved into variable and stored as long as you like. Your Startup configure method would look like:

public void Configure(IAppBuilder app)
{
    var resolver = new MyStructureMapResolver();

    // configure depdendency resolver
    GlobalHost.DependencyResolver = this.container.Resolve<IDependencyResolver>();

    // map the hubs
    app.MapSignalR();

    // get your hub context
    var hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();

    // register it in your structure map
    ObjectFactory.Inject<IHubContext>(hubContext);
}    

To have your hub context strongly typed, you can do something like that:

public interface INotificationHubContext {
    void NotifyOthersAllOnLogin(string msg);
}

Then you make this:

// get your hub context
var hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub, INotificationHubContext>();

// register it in your structure map
ObjectFactory.Inject<IHubContext<INotificationHubContext>>(hubContext);

Hope this helps.

Kędrzu
  • 2,385
  • 13
  • 22
  • I like your approach but, I already have DI implemented in my Project using IHubActivator... to Inject Dependencies creating hubs..... But Unable to inject HubInstances in Controller.... Your Apporach Shows thai i have to Register all my hubs Manually with the DI container..... Do you know how can you write a convention to inject he same... – Ankesh Sep 30 '14 at 10:13
  • SignalR is looking for hub types with a service IAssemblyLocator, which returns all assemblies to look into. Considering this fact, you can copy that behaviour to register all your hubs. – Kędrzu Sep 30 '14 at 14:53
  • And maybe I misunderstood you. You want to call hub methods, or client methods from your controller? – Kędrzu Sep 30 '14 at 14:57
  • I want to call Hub method,which in turn call ClientMethods,Also,I am bot able to locate which Assebly to add to get this `ConnectionManager.GetHubContext()` method ?? – Ankesh Oct 01 '14 at 05:02
  • 1
    You do not use hub instance to call client methods. Hub is just a transient class, which is created when client make his request. It is not that simple to just create Hub and call clients, because your newly created hub won't even have a proper client context. Instead of this, you need to use IHubContext, as I said. If you want to have stronly typed hub context, you need the latest version of SignalR. This is a feature that was just added, I think. This works very similiar to Castle Core DynamicProxy (maybe event this lib is used). – Kędrzu Oct 01 '14 at 08:11
  • Thanks for the Explanation, I got your point, Hubs are used by client calls and Server to client calls are done through `IHubContext` – Ankesh Oct 06 '14 at 08:57
  • The answer posted is the way to go. Just stumbled across this, was trying to validate that injecting IHubContext was the way to go. – user3210546 Feb 04 '15 at 01:19
  • I can confirm that adding GlobalHost.DependencyResolver = config.Resolver; solved the issue – Dany Ellement Jun 29 '17 at 21:08