Castle IInitializable异常未传播

时间:2011-11-24 16:41:17

标签: c# castle-windsor castle

考虑此代码:

public class A
{
    public B b { get; set; }
}

public class B : IInitializable
{
    #region IInitializable Members

    public void Initialize()
    {
        throw new NotImplementedException();
    }

    #endregion
}

class Program
{
    static void Main(string[] args)
    {
        WindsorContainer container = new WindsorContainer();
        container.Register(Component.For<A>());
        container.Register(Component.For<B>());

        try
        {
            A a = container.Resolve<A>();
            // goes here and a.b is null !!!
        }
        catch (Exception ex)
        {
            // never goes here :(
            Console.WriteLine(ex);
        }
    }
}

我原本期望将NotImplementedException传播到main catch。 相反,异常被windsor捕获,属性a.b为null ...

是否有任何想法让我的异常正确传播?

1 个答案:

答案 0 :(得分:0)

这是设计的。您正在解析A,其中B作为可选依赖项。由于异常,Windsor无法解析B,因此它将其视为不可解析,并且因为它是可选依赖项,所以没有错误,它只是继续而不注入B。

Here's a very similar situation, along with a workaround