我想我错过了一些东西,或者我正在做一些愚蠢的事情,但我无法理解它。假设以下两个类:
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
电话中删除了其他注册,因为它似乎没有相关性)
答案 0 :(得分:1)
我不知道该说什么,除了:在我的机器上工作(使用Windsor 3.2),使用原始(第一个)代码,我只是取消注释注释掉的代码。
答案 1 :(得分:0)
我刚试过一个类似的场景。我正在使用动态参数,一切都很好。
我添加了命名注册并使用DependsOn(ServiceOverride.ForKey)引用了一些注册,并且我得到了一大堆&#34;正在等待以下依赖项&#34;错误。
然后我将所有动态依赖项转换为匿名对象表单,一切都很好。我没有在这些步骤之间做任何其他事情。我使用Windsor版本3.3.0。我很高兴它终于奏效了。