我想我错过了一些东西,或者我正在做一些愚蠢的事情,但我无法理解它。假设以下两个类:
public class A
{
public A(string a, string b)
{
}
}
public class B
{
public B(A boris2)
{
}
}
而这个main
方法:
private static void Main(string[] args)
{
var container = new WindsorContainer();
container.Register(
Component.For<A>().LifeStyle.Transient.ImplementedBy<A>().Named("Boris2")
//.DynamicParameters((k,d)=>{
// d["b"] = "jkl";
//})
.DependsOn(new { a = "ghi", b="jkl" })
);
container.Register(
Component.For<B>().LifeStyle.Transient.ImplementedBy<B>()
.DependsOn(Dependency.OnComponent("boris2", "Boris2"))
);
var b = container.Resolve<B>();
Console.WriteLine("Okay");
Console.ReadKey();
}
如果我如上所示运行此方法,它会打印Okay
,然后按回车键,然后退出。一切皆好。但是,如注释掉的代码所示,我希望b
动态计算参数(最终会,但我只是想解决问题的核心)
所以,如果我第一次打电话Register
看起来像这样:
container.Register(
Component.For<A>().LifeStyle.Transient.ImplementedBy<A>().Named("Boris2")
.DynamicParameters((k, d) =>
{
d["b"] = "jkl";
})
.DependsOn(new { a = "ghi" })
);
我认为这大致等价,现在我得到一个例外:
Castle.MicroKernel.Handlers.HandlerException was unhandled
HelpLink=groups.google.com/group/castle-project-users
HResult=-2146233088
Message=Can't create component 'PlayAreaCS_Con.B' as it has dependencies to be satisfied.
'PlayAreaCS_Con.B' is waiting for the following dependencies:
- Component 'Boris2' (via override) which was registered but is also waiting for dependencies.
'Boris2' is waiting for the following dependencies:
- Parameter 'b' which was not provided. Did you forget to set the dependency?
Source=Castle.Windsor
StackTrace:
at Castle.MicroKernel.Handlers.DefaultHandler.AssertNotWaitingForDependency()
at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden)
at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context)
at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy)
at Castle.MicroKernel.DefaultKernel.Resolve(Type service, IDictionary arguments)
at Castle.Windsor.WindsorContainer.Resolve[T]()
at PlayAreaCS_Con.Program.Main(String[] args) in c:\Dev\PlayAreaCS_Con\PlayAreaCS_Con\Program.cs:line 49
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
我什至尝试将两个参数都移入DynamicParameters
和删除DependsOn
,认为将两者混合在一起可能会出现一些问题,但它仍然会出错。据我所知,它根本没有调用我的 lambda。
我究竟做错了什么?
编辑
如果我注释掉:
.DependsOn(Dependency.OnComponent("boris2", "Boris2"))
从我的第二个Register
电话开始,它就起作用了。但我确实需要在容器中注册几个相同的组件,仅按名称区分。(我已经从第一次Register
通话中删除了其他注册,因为它似乎不相关)