1
votes

I have a factory that takes in IEnumerable<IPredicate> in the constructor. My factory signature looks like this.

public ServiceFactory(IEnumerable<IPredicate> predicates)

And here is my Windsor registering code.

container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel, false));

container.Register(Component
    .For<IServiceFactory>()
    .ImplementedBy<ServiceFactory>());
container.Register(AllTypes.
    FromThisAssembly().
    BasedOn<IPredicate>().
    WithService.AllInterfaces());
container.Register(Component
    .For<IEnumerable<IPredicate>>());

Windsor throws an exception when I resolve IServiceFactory because it says I haven't registered constructor parameter predicates. But as you can see the third component I register is the thing it says I haven't registered.

The exception is:

Keys (components with specific keys)
- predicates which was not registered.

What am I missing?

The odd thing is that I originally wrote this code in a separate project and imported it into my production project. Is it possible that I have another installer interfering with this?

I'm using Castle Windsor v2.5.1.0.

2

2 Answers

2
votes

After reading my question for the 5346 time it finally clicked. The test project where I initially wrote this code was a simple one project test. However, my production project has several different projects in one solution. The things I was asking Windsor to resolve live in a separate assembly. My installer was telling Windsor to register all types based on IPredicate from the current assembly using FromThisAssembly(). Obviously, I told Windsor to find things it couldn't find.

The simple fix was to use FromAssemblyNamed("Your.Assembly.Name") instead.

So always check which assemblies you tell Windsor to check.

2
votes

Your other project most likely has this thing somewhere:

var kernel = container.Kernel;

kernel.Resolver.AddSubResolver(new CollectionResolver(kernel));

because this is the thing that makes Windsor do a ResolveAll for the type inside various collection types, including IEnumerable<T>.

And then you should remove the container.Register(Component.For<IEnumerable<IPredicate>>()) thing - that line doesn't really register anything, and that might be the reason why the collection resolver doesn't kick in.