You registered two closed-generic decorators, instead of the open-generic types. This is your registration:
container.RegisterDecorator(typeof(ICommandHandler<>),
typeof(LoggerCommandHandlerDecorator<Command>));
What this configuration says is, for every ICommandHandler<T>
implementation that is resolved, try to wrap it with an LoggerCommandHandlerDecorator<Command>
when possible. Since you supplied the closed LoggerCommandHandlerDecorator<Command>
however, it can only be applied to an ICommandHandler<Command>
implementation, but never to any subtype of command such as with ICommandHandler<MoveCustomerCommand>
.
The reason this is not possible, is because when someone requests an ICommandHandler<MoveCustomerCommand>
, it expects an implementation of this type, but LoggerCommandHandlerDecorator<Command>
does not implement ICommandHandler<MoveCustomerCommand>
; it only implements ICommandHandler<Command>
. So although a LoggerCommandHandlerDecorator<Command>
could wrap an ICommandHandler<MoveCustomerCommand>
, it can't be returned. It would cause an InvalidCastException
.
So this should have been your registration:
container.RegisterDecorator(typeof(ICommandHandler<>),
typeof(LoggerCommandHandlerDecorator<>));