温莎城堡释放组件

发布于 2024-10-10 07:31:20 字数 723 浏览 0 评论 0原文

编辑:

我已经重新表述了这个问题,因为我已经能够将我的问题简化为以下内容:

        var container = new WindsorContainer();

        container.Register(Component.For<IFoo>().ImplementedBy<Foo>().LifeStyle.Singleton);
        var foo = container.Resolve<IFoo>();
        container.Kernel.ReleaseComponent(foo);
        var foo2 = container.Resolve<IFoo>();

        Assert.IsTrue(foo != foo2) // this fails

public interface IFoo : IDisposable { }

public class Foo : IFoo {
    public void Dispose()
    {

    }
}

我一定在这里做了一些非常愚蠢的事情......知道什么吗?

基本上,我在这里想要完成的是让组件的所有分辨率在一段时间内(范围)返回相同的实例。如果有更好的方法来实现这一点,我当然愿意。

编辑: 好的,我是 RTFM,显然这是设计使然。实现我想要的目标的最佳方式仍然是自定义生活方式管理器吗?

谢谢。

EDIT:

I've completed rephrased the question as I've been able to simplify my problem down to the following:

        var container = new WindsorContainer();

        container.Register(Component.For<IFoo>().ImplementedBy<Foo>().LifeStyle.Singleton);
        var foo = container.Resolve<IFoo>();
        container.Kernel.ReleaseComponent(foo);
        var foo2 = container.Resolve<IFoo>();

        Assert.IsTrue(foo != foo2) // this fails

public interface IFoo : IDisposable { }

public class Foo : IFoo {
    public void Dispose()
    {

    }
}

I must be doing something really stupid here...any idea what?

Basically, what I'm trying to accomplish here is for a all resolutions of a component to return the same instance for some period of time (a scope). If there is a better way of accomplishing this, I'm certainly open to it.

EDIT:
Ok, so I RTFM and apparently this is by design. Is the best way of accomplishing what I want here still a custom lifestyle manager?

Thanks.

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

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

发布评论

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

评论(1

陈年往事 2024-10-17 07:31:20

您的 FooPerThread。这意味着如果您在另一个线程上请求它,您将获得一个新实例。如果我理解正确的话,问题在于你选择的生活方式,而不是你如何发布组件。

Your Foo is PerThread. That means you will get a new instance... if you request it on another thread. If I understand you correctly, the issue is with the lifestyle you've chosen, not how you release components.

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