I\'m trying to introduce domain events into a project. The concept is described in Udi Dahan\'s post - http://www.udidahan.com/2009/06/14/domain-events-salvation/
Here\'
As Peter pointed out, the registration problem was with your Where()
clause.
When scanning assemblies Autofac filters components automatically based on the services you specify, so it would be equally correct to just use:
builder.RegisterAssemblyTypes(asm)
.AsClosedTypesOf(handlerType)
.InstancePerLifetimeScope();
The problem in your assembly scanning code is when you use IsAssignableFrom
. The filter will ask: "could an instance of SendWebQuestionToCSO
be assigned to a variable of IHandleDomainEvents<>
?"
The answer is obviously "no" since you can never have a variable of an open generic type.
The trick would be to inspect the interfaces implemented by each type and check whether any of them are closing the open generic interface type. Here's a revised scanner:
var asm = Assembly.GetExecutingAssembly();
var handlerType = typeof(IHandleDomainEvents<>);
builder.RegisterAssemblyTypes(asm)
.Where(t => t.GetInterfaces().Any(t => t.IsClosedTypeOf(handlerType)))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();