views:

90

answers:

1

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 ReleaseComponent_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?

+1  A: 

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...

Krzysztof Koźmic
I've done that and it works.
Marius

related questions