【问题标题】:Castle IInitializable exception not propagatedCastle IIinitializable 异常未传播
【发布时间】:2011-11-24 16:41:17
【问题描述】:

考虑到这段代码:

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 传播到主要捕获。 相反,异常被windsor捕获,属性a.b为null...

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

【问题讨论】:

  • 你为什么想要那个例外,为什么 B 不是 A 的强制依赖?

标签: c# castle-windsor castle


【解决方案1】:

这是设计使然。您正在解析将 B 作为 optional 依赖项的 A。 Windsor由于异常无法解析B,所以将其视为不可解析,并且由于它是一个可选依赖,所以没有错误,它只是在不注入B的情况下继续。

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

【讨论】:

    猜你喜欢
    • 2016-06-24
    • 2015-12-25
    • 2016-04-10
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多