The dve answer is fine, but it's a bit confusing by adding another more specific answer. My main goal is to work:
[HubName("MyHub")] public class MyHub : Hub { public IJobRepository JobRepository { get; } public MyHub(IJobRepository jobRepository) { JobRepository = jobRepository ?? throw new ArgumentNullException(nameof(jobRepository)); } ... }
Of course, you want your hubs to be created for you, they are usually created by SignalR, but now that they have some dependencies, SignalR cannot create them. SignalR itself has a Resendver Dependency (in the SignalR namespace) that it uses to get its dependencies, you can add material to it, but we want Windsor to remember? Thus, we are going to change the way IHubActivator
creates hubs, we will not use SignalR, but this one:
public class SignalRHubActivator : IHubActivator { private readonly IWindsorContainer _container; public SignalRHubActivator(IWindsorContainer container) { _container = container; } public IHub Create(HubDescriptor descriptor) { var result = _container.Resolve(descriptor.HubType) as IHub; if (result is Hub) { _container.Release(result); } return result; } }
To replace this in the SignalR container, you should do something like:
// Get an instance of the hub creator (see note below) var _hubActivator = new SignalRHubActivator(container); // Get the SignalR Default Dependency Resolver var signalRResolver = new Microsoft.AspNet.SignalR.DefaultDependencyResolver(); // Override the IHubActivator service signalRResolver.Register(typeof(IHubActivator), () => _hubActivator); // now map SignalR with this configuration appBuilder.MapSignalR(new HubConfiguration { Resolver = signalRResolver });
And for this, you must also register all your hubs with Windsor
container.Register(Classes.FromThisAssembly() .BasedOn(typeof(Microsoft.AspNet.SignalR.Hub))); ... container.Register(Component.For<IJobRepository>()).ImplementedBy<JobRepository>());
Note: I registered SignalRHubActivator as a component because the Startup
class that I use gets the activator as a dependency:
container.Register(Component.For<SignalRHubActivator>(). DependsOn(Dependency.OnValue("container", container)));