Castle Windsor,钩子容器释放以便调用显式组件释放

发布于 2024-08-06 14:42:33 字数 554 浏览 4 评论 0原文

我在应用程序启动

public class ConfigurationFacility : AbstractFacility {
    private readonly List<string> configuredComponents = new List<string>();

    protected override void Init() {
        Kernel.ComponentRegistered += OnComponentRegistered;
        // add environment configurators
    }
    private void OnComponentRegistered(string key, IHandler handler) {
        // if the component is a configurator then run conf settings and add it to configuredComponents
    }}

问题:如何挂钩拆卸并为每个调用显式释放?

谢谢

I run this at the application Start Up

public class ConfigurationFacility : AbstractFacility {
    private readonly List<string> configuredComponents = new List<string>();

    protected override void Init() {
        Kernel.ComponentRegistered += OnComponentRegistered;
        // add environment configurators
    }
    private void OnComponentRegistered(string key, IHandler handler) {
        // if the component is a configurator then run conf settings and add it to configuredComponents
    }}

Question: How to hook tear down and to call explicit release for each ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你没皮卡萌 2024-08-13 14:42:33

您可以使用 IKernelComponentDestroyed 事件,也可以在组件中实现 IDisposable。下面是一些示例代码:

namespace WindsorInitConfig {
    [TestFixture]
    public class ConfigurationFacilityTests {
        [Test]
        public void tt() {
            OneDisposableComponent component = null;
            using (var container = new WindsorContainer()) {
                container.AddFacility<ConfigurationFacility>();
                container.AddComponent<OneDisposableComponent>();
                component = container.Resolve<OneDisposableComponent>();
            }
            Assert.IsTrue(component.Disposed);
            Assert.Contains(component, ConfigurationFacility.DestroyedComponents);
        }

        public class OneDisposableComponent : IDisposable {
            public bool Disposed { get; private set; }

            public void Dispose() {
                Disposed = true;
            }
        }

        public class ConfigurationFacility : AbstractFacility {
            private readonly List<string> configuredComponents = new List<string>();
            public static readonly ArrayList DestroyedComponents = new ArrayList();

            protected override void Init() {
                Kernel.ComponentRegistered += OnComponentRegistered;
                Kernel.ComponentDestroyed += Kernel_ComponentDestroyed;
                // add environment configurators
            }

            private void Kernel_ComponentDestroyed(ComponentModel model, object instance) {
                DestroyedComponents.Add(instance);
                // uninitialization, cleanup
            }

            private void OnComponentRegistered(string key, IHandler handler) {
                // if the component is a configurator then run conf settings and add it to configuredComponents
                configuredComponents.Add(key);}
        }
    }
}

当然,静态 ArrayList 仅用于演示目的。

You can use either the ComponentDestroyed event of IKernel or just implement IDisposable in your components. Here's a little sample code:

namespace WindsorInitConfig {
    [TestFixture]
    public class ConfigurationFacilityTests {
        [Test]
        public void tt() {
            OneDisposableComponent component = null;
            using (var container = new WindsorContainer()) {
                container.AddFacility<ConfigurationFacility>();
                container.AddComponent<OneDisposableComponent>();
                component = container.Resolve<OneDisposableComponent>();
            }
            Assert.IsTrue(component.Disposed);
            Assert.Contains(component, ConfigurationFacility.DestroyedComponents);
        }

        public class OneDisposableComponent : IDisposable {
            public bool Disposed { get; private set; }

            public void Dispose() {
                Disposed = true;
            }
        }

        public class ConfigurationFacility : AbstractFacility {
            private readonly List<string> configuredComponents = new List<string>();
            public static readonly ArrayList DestroyedComponents = new ArrayList();

            protected override void Init() {
                Kernel.ComponentRegistered += OnComponentRegistered;
                Kernel.ComponentDestroyed += Kernel_ComponentDestroyed;
                // add environment configurators
            }

            private void Kernel_ComponentDestroyed(ComponentModel model, object instance) {
                DestroyedComponents.Add(instance);
                // uninitialization, cleanup
            }

            private void OnComponentRegistered(string key, IHandler handler) {
                // if the component is a configurator then run conf settings and add it to configuredComponents
                configuredComponents.Add(key);}
        }
    }
}

The static ArrayList is only for demo purposes, of course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文