We have a complex application with hubs spread accross multiple dlls. The complete application uses DI with LightCore. Now we face the problem that SignalR doesn't seem to use the DependencyResolver Adapter we have written.
We have tried two different ways to inject our custom IDependencyResolver:
GlobalHost.DependencyResolver = new LightCoreSignalRDependencyResolver();
and
var hc = new HubConfiguration(Resolver = new LightCoreSignalRDependencyResolver());
app.MapSignalR(hc);
In both cases, our resolver is not called when a hub is created. Even worse, we get exceptions that there is no parameterless constructor for the hubs.
We also did a short test with just injecting a custom IHubActivator into the DefaultDependencyResolver. Same result. The HubActivator is not used at all - at least we do not run into our breakpoints and log entries.
Does anybody have an idea what could go wrong here?
(In MVC and WebApi the custom dependency resolvers work as expected)
Hubs are register using a builder helper:
builder.Register<Hubs.LoggingHub, Hubs.LoggingHub>();
The Dependency Resolver looks like this:
public class LightCoreSignalRDependencyResolver : DefaultDependencyResolver
{
private readonly IContainer _container;
public LightCoreSignalRDependencyResolver(IContainer container)
{
if (container == null) throw new ArgumentNullException("container");
_container = container;
}
public object GetService(Type serviceType)
{
try
{
return _container.Resolve(serviceType);
}
// IDependencyResolver implementations must not throw an exception
// but return null if type is not registered
catch (RegistrationNotFoundException registrationNotFoundException)
{
return base.GetService(serviceType);
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return _container.ResolveAll(serviceType).Concat(base.GetServices(serviceType));
}
// IDependencyResolver implementations must not throw an exception
// but return an empty object collection if type is not registered
catch (RegistrationNotFoundException registrationNotFoundException)
{
return base.GetServices(serviceType);
}
}
}
Hub constructors look like this:
This one is used with the default resolver and uses our internal Bootstrapper to resolve the dependencies. (the ugly but working way - showing that the dependencies can be resolved)
public RobotControlHub()
{
_Publisher = Booty.Container.Resolve<IPublisher>();
_MessageService = Booty.Container.Resolve<IRobotMessageRepectionService>();
_CommandsService = Booty.Container.Resolve<IRobotGenericCommandsService>();
_ConnectionsMapping = Booty.Container.Resolve<IRobotConnectionMappingRepository>();
}
Thats the constructor we would expect to be called when using our Resolver within SignalR.
public RobotControlHub(
IRobotConnectionMappingRepository connectionsMapping,
IRobotGenericCommandsService commandsService,
IRobotMessageRepectionService messageService,
IPublisher publisher)
{
_Publisher = publisher;
_MessageService = messageService;
_CommandsService = commandsService;
_ConnectionsMapping = connectionsMapping;
}
But as said, it seems it is not even called.
Hub
s? – rae1