温莎城堡释放组件
编辑:
我已经重新表述了这个问题,因为我已经能够将我的问题简化为以下内容:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
Foo
是PerThread
。这意味着如果您在另一个线程上请求它,您将获得一个新实例。如果我理解正确的话,问题在于你选择的生活方式,而不是你如何发布组件。Your
Foo
isPerThread
. 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.