我想我正在丢失某些东西,或者我正在某种程度上做一些愚蠢的事情,但是我无法理解。假定以下两个类:
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
调用中删除了其他注册,因为它似乎无关紧要) 最佳答案
我不知道该说些什么,除了:在我的机器上(使用Windsor 3.2)可以使用原始(第一个)代码运行,而在这里我只是注释掉了注释过的代码。
关于c# - 如何混合DynamicParameters和DependsOn,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15274103/