Issue with resolving a type in castle that depends on IList
I am trying to resolve an object (Context class) which has a constructor dependency on an IList but I am not able to resolve( see exception below). Can anyone explain why is this? Is there an issue with castle and .net generic exceptions? Thanks for replies
public class Context : IContext
{
public Context(
IApplication开发者_如何学编程Site applicationSite,
IList<Currency> currencies)
{
}
}
And this is my IOC registration:
var _container = new WindsorContainer();
_container.Kernel.AddFacility<FactorySupportFacility>();
_container.Register(
Component.For<IRepository>()
.ImplementedBy<Repository>()
.Named("repository"));
_container.Register(
Component
.For<IList<Currency>>()
.UsingFactoryMethod(kernel => kernel.Resolve<IRepository>().GetCurrencies())
.Named("currencies"));
_container.Register(
Component
.For<IApplicationSite>()
.UsingFactoryMethod(kernel => kernel.Resolve<IRepository>().GetApplicationSite())
.Named("applicationSite"));
_container.Register(
Component.For<IContext>()
.ImplementedBy<Context>()
.Named("Context"));
var _context = _container.Resolve<IContext>();
When I try to resolve context I get the following exception:
Castle.MicroKernel.Resolvers.DependencyResolverException : Could not resolve non-optional dependency for 'Context' (ClassLibraryCastle.Context).
Parameter 'currencies' type 'System.Collections.Generic.IList`1[[ClassLibraryCastle.Currency, ClassLibraryCastle, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'
I have found a method of resolving a dependency on IList - using dyanamic parameters
_container.Register(
Component.For<IContext>()
.ImplementedBy<Context>()
.Named("context")
.DynamicParameters((k, parameters) => // dynamic parameters
{
parameters["currencies"] = k.Resolve<IList<Currency>>();
}));
精彩评论