4

我想我错过了一些东西,或者我正在做一些愚蠢的事情,但我无法理解它。假设以下两个类:

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通话中删除了其他注册,因为它似乎不相关)

4

2 回答 2

1

我不知道该说什么,除了:在我的机器上工作(使用 Windsor 3.2),使用原始(第一个)代码,我只是取消了注释掉的代码。

于 2013-03-07T23:41:42.963 回答
0

我刚刚尝试了类似的场景。我正在使用动态参数,一切都很好。

我添加了命名注册并使用 DependsOn(ServiceOverride.ForKey) 引用了一些注册,我得到了一大堆“正在等待以下依赖项”错误。

然后我将所有动态依赖项转换为匿名对象形式,一切都很好。在这些步骤之间我没有做任何其他事情。我使用的是温莎 3.3.0 版。我很高兴它终于奏效了。

于 2016-01-26T23:45:30.950 回答