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 V3
for DependencyInjection.
Any Help/direction in this regard will be appreciated
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.