1

I'm on an MVC3 project and I am using snap structuremap for my dependecy injection. Everything was in-place, except when I started using SignalR where I can't seem to implement my DI like I have on my controllers. I've been googling about implementing structuremap DI on SignalR for days now, but haven't found a strong sample on how to do this. Seems like everyone that are using SignalR are using Ninject.

My goal is to have conditional statements (which requires me to inject services) inside my Hub before calling my client methods, but I had no success on this.

I didn't want this thing to delay my development so I researched for alternative ways, then I found out that I can actually call my client methods from my controllers using the following codes:

 IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
 dynamic clients = connectionManager.GetClients<MyHub>();
 clients.myClientScript();

This works for me, but I'm not sure if this is a good approach - especially that I am using dependency injection.

So my question is: Is it ok to keep calling this inside my controller? Do you have a better approach?

Thanks

dmc
  • 807
  • 2
  • 10
  • 25
  • I missed your other question (http://stackoverflow.com/q/9790433/701062). Moved the structuremap example there. – Gary.S Mar 26 '12 at 03:56

1 Answers1

1

There is no reason you cannot send information to connected clients from your controller using SignalR however the current client will not see this information (due to not being connected during a post).

That said, getting Structuremap into SignalR is actually pretty easy. You can see exactly how to accomplish this in my answer here: https://stackoverflow.com/a/9866374/701062.

Community
  • 1
  • 1
Gary.S
  • 7,076
  • 1
  • 26
  • 36
  • Thanks @Gary.S . I actually have tried implementing structuremap with signalR this way but I used var container = (IContainer) IoC.Initialize(); AspNetHost.SetResolver(new StructureMapDependencyResolver(container)); in AppStart. – dmc Mar 26 '12 at 04:04
  • Now I tried your AspNetHost.SetResolver(StructureMap.ObjectFactory.GetInstance()); on AppStart but I'm getting Invalid Arguments – dmc Mar 26 '12 at 04:05