I am trying to decorate my command handlers and I am trying to resolve them in my processor.
I registered my command like this:
builder.RegisterAssemblyTypes(typeof(ICommandProcessor).Assembly)
.AsClosedTypesOf(typeof(ICommandHandler<,>))
.AsSelf().AsImplementedInterfaces().Named("implementor", typeof(ICommandHandler<,>));
builder.RegisterGenericDecorator(
typeof(CatchValidationErrorsDecorator<,>),
typeof(ICommandHandler<,>), fromKey: "implementor")
.AsImplementedInterfaces();
The problem is that when i am not using the Named extension, the generic decorator is not working. When I use the named extension, I am not able to resolve my components like this:
var handerType = typeof (ICommandHandler<,>)
.MakeGenericType(command.GetType(), typeof (TResult));
dynamic handler = _container.Resolve(handerType);
Someone has an idea how to solve this?
This has given me serious headaches in the past. The registration that eventually did it for me was:
var assembly = typeof(ICommandProcessor).Assembly);
builder.RegisterAssemblyTypes(assembly).As(type =>
from interfaceType in type.GetInterfaces()
where interfaceType.IsClosedTypeOf(typeof(ICommandHandler<,>))
select new KeyedService("implementor", interfaceType));
builder.RegisterGenericDecorator(
typeof(CatchValidationErrorsDecorator<,>),
typeof(ICommandHandler<,>),
fromKey: "implementor");