c# - Asp.Net SignalR DependencyResolver not resolving hubs -
we have complex application hubs spread accross multiple dlls. complete application uses di lightcore. face problem signalr doesn't seem use dependencyresolver adapter have written.
we have tried 2 different ways inject our custom idependencyresolver:
globalhost.dependencyresolver = new lightcoresignalrdependencyresolver();
and
var hc = new hubconfiguration(resolver = new lightcoresignalrdependencyresolver()); app.mapsignalr(hc);
in both cases, our resolver not called when hub created. worse, exceptions there no parameterless constructor hubs.
we did short test injecting custom ihubactivator defaultdependencyresolver. same result. hubactivator not used @ - @ least not run our breakpoints , log entries.
does have idea go wrong here?
(in mvc , webapi custom dependency resolvers work expected)
hubs register using builder helper:
builder.register<hubs.logginghub, hubs.logginghub>();
the dependency resolver looks 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 exception // return null if type 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 exception // return empty object collection if type not registered catch (registrationnotfoundexception registrationnotfoundexception) { return base.getservices(servicetype); } } }
hub constructors this:
this 1 used default resolver , uses our internal bootstrapper resolve dependencies. (the ugly working way - showing dependencies can resolved)
public robotcontrolhub() { _publisher = booty.container.resolve<ipublisher>(); _messageservice = booty.container.resolve<irobotmessagerepectionservice>(); _commandsservice = booty.container.resolve<irobotgenericcommandsservice>(); _connectionsmapping = booty.container.resolve<irobotconnectionmappingrepository>(); }
thats constructor expect 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 said, seems not called.
sometimes it's sleep night , forget issue. next morning @ code , see there "override" missing. correct code:
public class lightcoresignalrdependencyresolver : defaultdependencyresolver { private readonly icontainer _container; public lightcoresignalrdependencyresolver(icontainer container) { if (container == null) throw new argumentnullexception("container"); _container = container; } public **override** object getservice(type servicetype) { try { return _container.resolve(servicetype); } // idependencyresolver implementations must not throw exception // return null if type not registered catch (registrationnotfoundexception registrationnotfoundexception) { return base.getservice(servicetype); } } public **override** ienumerable<object> getservices(type servicetype) { try { return _container.resolveall(servicetype).concat(base.getservices(servicetype)); } // idependencyresolver implementations must not throw exception // return empty object collection if type not registered catch (registrationnotfoundexception registrationnotfoundexception) { return base.getservices(servicetype); } } }
Comments
Post a Comment