Windsor Castle orders

I have a problem removing the order of allowed components using Windsor Castle. The problem can be demonstrated on the following code

class Program
{
    static void Main(string[] args)
    {
        using (WindsorContainer container = new WindsorContainer())
        {
            container.Register(Component.For<C1>().LifestyleSingleton());
            container.Register(Component.For<C2>().LifestyleTransient());
            C1 c1 = container.Resolve<C1>();
            container.Release(c1);
            Console.WriteLine("Release done");
        }
        Console.WriteLine("Container dispose done");
    }
}

public class C1 : IDisposable
{
    private C2 m_c2;

    public C1(C2 c2)
    {
        m_c2 = c2;
    }

    public void Dispose()
    {
        Console.WriteLine("Dispose C1");
    }
}

public class C2 : IDisposable
{
    public void Dispose()
    {
        Console.WriteLine("Dispose C2");
    }
}

It prints the following data:

Release done
Dispose C2
Dispose C1
Dispose C2
Container dispose done

I expect the following output:

Release done
Dispose C1
Dispose C2
Container dispose done

Eliminating C2 to C1 can cause a serious problem in C1. C1 can still be alive and recycle something. Note that the problem disappears when both components are registered in a LifestyleSingleton or LifestyleTransient, but there are still cases where registering C2 as a LifestyleTransient can be useful.

Is there a way to register or enable components to solve this problem?

+4
1

, C1 . . , .

0

Source: https://habr.com/ru/post/1570887/


All Articles