c# - Windsor Container 什么时候触发临时组件的 IKernel.ComponentDestroyed 事件?

标签 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事件什么时候触发?

最佳答案

Mauricio 是对的 - 该组件不会触发该事件,因为该组件未被跟踪并且 Windsor 无论如何都不会与它有任何关系。

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

Windsor 3 推出时,有一个 OnDestroy 方法,该方法接受一个 lambda 并添加一个退役步骤,从而强制跟踪该组件,这意味着您放入其中的代码将被调用发布时。

关于c# - Windsor Container 什么时候触发临时组件的 IKernel.ComponentDestroyed 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5803645/

相关文章:

c# - string.Replace 不适用于报价

c# - 温莎容器生活方式问题

c# - 在 C# 中重绘气球提示和工具提示?

c# - 来自 DLL 的 Microsoft Visual Studio 2013 摘要

c# - WebRequest.DefaultWebProxy 和 WebRequest.GetSystemWebProxy() 有什么区别?

dependency-injection - 温莎城堡传递构造函数参数

C#、CaSTLe Windsor 和复合设计模式

c# - 如何使用 LINQ 将日期与毫秒进行比较

c# - 当 PreviewMouseLeftButtonDownCommand RelayCommand\EventToCommand 触发时,在 ItemsControl 中查找所选项目

.net - 自定义 WCF 扩展的单元测试,例如自定义行为和检查器