开发者

Windsor: How can I make windsor dispose a transient component when not tracking component lifecycle?

We are using the NoTrackingReleasePolicy on the Windsor container due to the memory leaks that occur when we do not Release our components after usage. Now consider the following problem.

Some disposable component:

public class DisposableComponent : IDisposable
{
    private bool _disposed;

    public bool Disposed
    {
        get { return _disposed; }
    }

    public void Dispose()
    {
        _disposed = true;
        GC.SuppressFinalize(this);
    }
}

Some class using the disposable component:

public class ClassWithReferenceToDisposableService
{
    private DisposableComponent _disposableComponent;

    public ClassWithReferenceToDisposableService(DisposableComponent disposableComponent)
    {
        _disposableComponent = disposableComponent;
    }
}

And finaly a test which configures these components as transient and resolve/release them:

    [Test]
    public void ReleaseCo开发者_如何学Gomponent_ServiceWithReferenceToTransientDisposable_TransientComponentDisposed()
    {
        // arrange
        var windsorContainer = new WindsorContainer();

        windsorContainer.Kernel.ReleasePolicy = new NoTrackingReleasePolicy();

        windsorContainer.Register(Component.For<ClassWithReferenceToDisposableService>().LifeStyle.Transient);
        windsorContainer.Register(Component.For<DisposableComponent>().LifeStyle.Transient);

        ClassWithReferenceToDisposableService service =
            windsorContainer.Resolve<ClassWithReferenceToDisposableService>();

        // act
        windsorContainer.Release(service);
    }

Now, if I remove the NoTrackingReleasePolicy, Windsor will dispose the transient service as expected, but I can not do this (period). Now, what I want to achieve is that Windsor disposes the transient components (anywhere in the resolve graph) when I invoke ReleaseCompnent. Is there any way to achieve this without changing the NoTrackingReleasePolicy?


No, you can't have your cake and eat it too. You can implement your own custom policy that is kind of like NoTrackingReleasePolicy but will track some components...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜