Castle IInitialized 异常未传播
考虑到这段代码:
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 会传播到主捕获。 相反,异常被温莎捕获并且属性 ab 为空...
有什么办法让我的异常正确传播吗?
Considering this code :
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);
}
}
}
I would have expected the NotImplementedException to be propagated to the main catch.
Instead, the exception is caught by windsor and the property a.b is null...
Any idea to get my exception correctly propagated ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是设计使然。您正在解析 A,其中 B 作为可选依赖项。由于异常,Windsor 无法解析 B,因此将其视为不可解析,并且由于它是一个可选依赖项,因此没有错误,它只是继续执行而不注入 B。
这是一个非常类似的情况,以及解决方法。
This is by design. You're resolving A which has B as an optional dependency. Windsor can't resolve B because of the exception, so it treats it as non-resolvable, and since it's an optional dependency, there is no error, it just carries on without injecting B.
Here's a very similar situation, along with a workaround.