ActivationException decorator match IHandleMessages<T> but you registered IEnumerable<IHandleMessages<T>>
I'm using version 3.2.3 and I'm randomly (not always) getting this error:
SimpleInjector.ActivationException: The constructor of type HandlerLogDecorator<ProductionCorrectedEvent> contains the parameter with name 'decorated' and type IHandleMessages<ProductionCorrectedEvent> that is not registered. Please ensure IHandleMessages<ProductionCorrectedEvent> is registered, or change the constructor of HandlerLogDecorator<ProductionCorrectedEvent>. There is, however, a registration for IEnumerable<IHandleMessages<ProductionCorrectedEvent>>; Did you mean to depend on IEnumerable<IHandleMessages<ProductionCorrectedEvent>>?
I need a decorator of a single handler not a collection. This is how the decorator is like:
public class HandlerLogDecorator<TMessage> : IHandleMessages<TMessage>
where TMessage : IMessage
{
readonly IHandleMessages<TMessage> _decorated;
readonly ILog _log;
public HandlerLogDecorator(IHandleMessages<TMessage> decorated, ILogManager logManager)
{
_decorated = decorated;
_log = logManager.GetLogger(decorated.GetType().GenericName());
}
public async Task Handle(TMessage message)
{
...
}
}
and this is how I register decorator and handlers (note: I need a new container for each event handler):
Main method:
IEnumerable<Type> eventHandlers = assemblies
.GetHandlers(serviceType, typeof(IVersionedEvent))
.Except(commandHandlers)
//.Except(new List<Type> { typeof(TempHandlerLogDecorator) })
;
foreach (var handler in eventHandlers)
supensionService.AddServiceToSuspend(
ConfigureServiceBus(mongoDbConnectionProvider, db, handler.FullName, new[] { handler }));
Inside ConfigureServiceBus:
container.RegisterDecorator(typeof(IHandleMessages<>), typeof(HandlerLogDecorator<>));
container.RegisterCollection(typeof(IHandleMessages<>), handlers);
What I'm doing wrong? Thank you.
Try upgrating to Simple Injector v4.0. If the problem remains, please let me know.
I think you are hitting a bug that was fixed in v3.2.5. This bug appeared under the following conditions:
- Multiple container instances were used
- Container(s) contain decorator registrations
Container.Verify()was not called.
To fix, do either one of the following, migrate to at least v3.2.5, but it's better to migrate to the latest v3 release.
Other tips:
- You should typically create a single Container instance for the whole application (one instance per app domain); Container instances are thread-safe. But if you have to create more (for instance, when your AppDomain hosts a few highly isolated modules), you should only create a handful, and never an endless amount of instances. For instance, do not create a container instance for each web request.
- Make sure you call
Container.Verify()after you're done configuring the container. Verification detects many common configuration errors that are otherwise really hard to find.Verify()can be a true time-saver. - Upgrade to the latest v4 release. There have been introduced many bug fixes and improvements since v3.2.3. Don't stick to old versions for too long if you can.
I'm closing this issue now, but feel free to comment if you experience this bug after upgraded to the latest v4 version.
I've updated to the latest 3 version (3.3.2) and, since the bug is random, I'll publish in quality environment and I'll wait to see if it comes out again.
Anyway I need to clarify some conditions:
container.Verify()is called on each container (I simply omitted what was not inherent to the question).- The number of containers is 10: one for each topic, each topic is on a separate queue and I configure a container for each dequeuing process.
- I suppose I'm hitting the issue #420 because have to call
container.RegisterCollection(typeof(IHandleMessages<>), handlers);where handlers is an IEnumerable containing a single handler. Thank you.
Hi ilcorvo,
- Great to hear you're actually calling
Verify(). - Ah, okay, so you're having a finite amount of containers, each operating on its own isolated area on the application. That's good.
- #420 does not apply to
RegisterCollection.RegisterCollectioncan already handle open generic types (or at least, in v4.x it can, but I think this also holds in v3). You can do this:container.RegisterCollection(typeof(IHandler<>), new[] { typeof(HandleA), typeof(HandleB(), typeof(HandleAll<>) }). In that case a closed-generic version ofHandleAll<T>will be appended to each resolved collection forIHandler<T>.
If the bug still remains in v3.3.2, please upgrade to the latest v4.x release and try again. Much has changed in v4. There are a lot of small improvements and complete parts have been refactored, so it's very well possible that the problem is non-existent in v4.
Thank you Steven,
Unfortunately is not so easy to update to v4: I tried months ago, but there was an issue with ReBus SimpleInjectorAdapater so I gave up, holding for a while. I'll try again as soon as possible.
In the meanwhile some detail on point 3:
I already do exactly
container.RegisterCollection(typeof(IHandler<>), new[] { typeof(ConcreteHandleAll) }
where ConcreteHandleAll is the (only one per topic => container) concrete class that implements IHandler<T>.
The received SimpleInjector exception suggests:
Did you mean to depend on IEnumerable<IHandleMessages
>?
As if the error is related to the fact that I have registered a collection and not a single item …
This makes me think that I need a registration like:
container.Register(typeof(IHandler<>), typeof(ConcreteHandleAll))
I tried months ago, but there was an issue with ReBus SimpleInjectorAdapater so I gave up
You should try asking about this on the Rebus forum. If you and the Rebus maintainer can't figure it out, you can @ me to the discussion.
Did you mean to depend on IEnumerable
?
Well, that's exactly what the error message says. Simple Injector makes a very clear distinction between collection registrations and one-to-one mappings. So if you depend on IHandleMessages<T>, while you call RegisterCollection(typeof(IHandleMessages<T>)) you will absolutely get this exception. That is deliberate design decision, and is will not change.
So RegisterCollection is not so much about batch-registration, it's about registering a set of types that can be resolved as IEnumerable<T>. If you have a set of types that implement a some generic abstraction (e.g. IHandleMessages<T>) and for each specific T there is only 1 non-generic implementation, that's when you use Register(typeof(IHandleMessages<>), types). That method overload does batch-registration.
If you have a set of types that implement a some generic abstraction (e.g. IHandleMessages
) and for each specific T there is only 1 non-generic implementation, that's when you use Register(typeof(IHandleMessages<>), types).
I Have just the opposite: for each topic, I have only 1 handler that implements many IHandleMessages<T>.
But public methods are:
public void Register(Type openGenericServiceType, IEnumerable<Assembly> assemblies);
public void Register(Type openGenericServiceType, IEnumerable<Assembly> assemblies, Lifestyle lifestyle);
public void Register(Type openGenericServiceType, IEnumerable<Type> implementationTypes, Lifestyle lifestyle);
it looks like there is no specific registration for my case.
There is only one question that is important:
-> Do you need to resolve an collection of things (i.e. IEnumerable<T> ) or always just one instance of the closed-generic abstraction.
With the former you use RegisterCollection, with the latter you use Register.
It doesn't matter if you have one class that implements IHandleMessages<T> many times. As long as there are not multiple registrations for a specific T, you call Register.
With the following types for instance:
class X : IHandleMessages<A>, IHandleMessages<B>, IHandleMessages<C> { }
class Y : IHandleMessages<D>, IHandleMessages<E>, IHandleMessages<F> { }
You simply do this:
container.Register(typeof(IHandleMessages<>), new[] { typeof(X), typeof(Y) });
Or more conveniently:
container.Register(typeof(IHandleMessages<>), new[] { typeof(X).Assembly });
Both calls result in 6 registrations in the container, which is the equivalent of doing the following:
container.Register<IHandleMessages<A>, X>();
container.Register<IHandleMessages<B>, X>();
container.Register<IHandleMessages<C>, X>();
container.Register<IHandleMessages<D>, Y>();
container.Register<IHandleMessages<E>, Y>();
container.Register<IHandleMessages<F>, Y>();
If on the other hand, you have the following classes:
class X : IHandleMessages<A>, IHandleMessages<B>, IHandleMessages<C> { }
class Y : IHandleMessages<A>, IHandleMessages<E>, IHandleMessages<F> { }
Things get very different, because there are multiple implementations for IHandleMessages<A>. In that case there is not a one-to-one mapping between an abstraction (IHandleMessages<A>) and an implementation (both X and Y implement IHandleMessages<A>).
In that case you are dealing with an abstraction that is expected to have zero or more implementations (compared to exactly one). For that scenario RegisterCollection exists. You'll register this as follows:
container.RegisterCollection(typeof(IHandleMessages<>), new[] { typeof(X).Assembly });
That would result in 5 registrations, namely:
IEnumerable<IHandleMessages<A>>with 2 elementsIEnumerable<IHandleMessages<B>>with 1 elementIEnumerable<IHandleMessages<C>>with 1 elementIEnumerable<IHandleMessages<E>>with 1 elementIEnumerable<IHandleMessages<F>>with 1 element
You would resolve such collection using:
IEnumerable<IHandleMessages<A>> col = container.GetAllInstances<IHandleMessages<A>>();
// or
var col = container.GetInstance<IEnumerable<IHandleMessages<A>>>();
// or when injecting into a constructor
public SomeType(IEnumerable<IHandleMessages<A>> handlers) { ... }
When dealing with collections, you can also add open-generic registrations to the collection, for instance:
container.RegisterCollection(typeof(IHandleMessages<>), new[] { typeof(X).Assembly });
container.AppendToCollection(IHandleMessages<>), typeof(OpenHandler<>));
That would result in the following registrations, namely:
IEnumerable<IHandleMessages<A>>with 3 elements (X,YandOpenHandler<A>)IEnumerable<IHandleMessages<B>>with 2 element (XandOpenHandler<B>)IEnumerable<IHandleMessages<C>>with 2 element (XandOpenHandler<C>)IEnumerable<IHandleMessages<E>>with 2 element (YandOpenHandler<E>)IEnumerable<IHandleMessages<F>>with 2 element (YandOpenHandler<F>)- Any other resolved collection will have 1 element, namely a closed version of
OpenHandler<T>for that specificT.