Windsor Container何时触发瞬态组件的IKernel.ComponentDestroyed事件?

时间:2011-04-27 11:53:42

标签: c# .net castle-windsor

调用container.Release时不会触发事件,以下测试总是失败:

public void ComponentDestroyedEvent()
{
  var wasDestroyed = false;

  var container = new WindsorContainer()
    .Register(
      Component.For(typeof (Cat))
        .LifeStyle.Transient
        .OnCreate((k, instance) => {
          k.ComponentDestroyed += (model, component) => {
            if (component == instance)
              wasDestroyed = true;
          };
        }));


  var cat = container.Resolve<Cat>();
  container.Release(cat);

  Assert.True(wasDestroyed);
}

什么时候触发了ComponentDestroyed事件?

1 个答案:

答案 0 :(得分:4)

Mauricio是对的 - 该组件未触发该事件,因为该组件未被跟踪,Windsor也无法与其进行任何关联。

在跟踪组件的情况下,在运行所有停用步骤之后,将事件作为管道的最后一步引发。

Windsor 3,当它出来时,有一个OnDestroy方法,它接受一个lambda并添加一个decommission步骤,从而强制跟踪组件,这意味着你放在那里的代码将被释放时调用。

相关问题