Castle Windsor: Release component bug?

72 views
Skip to first unread message

mynkow

unread,
Jul 19, 2011, 10:39:00 AM7/19/11
to castle-pro...@googlegroups.com
Hi

I have a transient object TObj1 which depends on singleton object SObj1. SObj1 is supposed to be UP in 99.99% of the application life time. Now I am in a situation where the singleton cannot be created. If that happens I set SObj1 = null in TObj1's ctor when I try to resolve an instance of TObj1. When I try to release TObj1 (because it is transient) I got a Castle exception that parameter "instance" cannot be null. The only thing that is null is SObj1. As far as I know singleton objects cannot be released. They are "destroyed" when we "destroy" the container. Then Why castle windsor actually tries to release the singleton? Is there any way to release only the transient?

Mauricio Scheffer

unread,
Jul 19, 2011, 11:32:30 AM7/19/11
to castle-pro...@googlegroups.com

Can you create a failing test to reproduce this? Also, if sobj1 is an optional dependency of tobj1, it should probably be modeled as a property, not as a ctor arg...

On 2011 7 19 11:39, "mynkow" <myn...@gmail.com> wrote:
> Hi
>
> I have a transient object *TObj1 *which depends on singleton object SObj1.
> SObj1 is supposed to be UP in 99.99% of the application life time. Now I am
> in a situation where the singleton cannot be created. If that happens I set
> SObj1 = null in TObj1's ctor when I try to resolve an instance of TObj1.
> When I try to release TObj1 (because it is transient) I got a Castle
> exception that parameter "*instance*" cannot be null. The only thing that is
> null is SObj1. As far as I know singleton objects cannot be released. They
> are "destroyed" when we "destroy" the container. Then Why castle windsor
> actually tries to release the singleton? Is there any way to release only
> the transient?
>
> --
> You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/castle-project-users/-/KtfSwkD21a0J.
> To post to this group, send email to castle-pro...@googlegroups.com.
> To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.
>

mynkow

unread,
Jul 19, 2011, 11:57:54 AM7/19/11
to castle-pro...@googlegroups.com
Sure, here is the failing:

public class Transient
    {
        private readonly Singleton _singleton;
        public Transient(Singleton singleton)
        {
            _singleton = singleton;
        }
    }

    public class Singleton
    {
        public Singleton()
        {
            throw new Exception("Cannot create singleton object.");
        }
    }

private void test()
        {
            IWindsorContainer testContainer = new WindsorContainer();
            testContainer.Register(
                Component.For<Singleton>(),
                Component.For<Transient>().LifeStyle.Transient);

            Transient transientInstance = null;
            try
            {
                transientInstance = testContainer.Resolve<Transient>();
            }
            catch
            {
                //  Do something here.
            }
            finally
            {
                testContainer.Release(transientInstance);
                testContainer.Dispose();
            }

        }

Krzysztof Koźmic

unread,
Jul 19, 2011, 5:26:43 PM7/19/11
to castle-pro...@googlegroups.com
There's no way to create Transient without supplying Singleton and
there's no way to create singleton because it throws. In other words
it's a design issue, not Windsor's

> --
> You received this message because you are subscribed to the Google
> Groups "Castle Project Users" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/castle-project-users/-/p0jiAQn5V40J.

mynkow

unread,
Jul 19, 2011, 5:46:45 PM7/19/11
to castle-pro...@googlegroups.com
Ok, I see.

But if I get in this situation what is happening with the transient if it cannot be released? Is the object kept by windsor or it can be GCed?

Krzysztof Koźmic

unread,
Jul 19, 2011, 6:12:32 PM7/19/11
to castle-pro...@googlegroups.com
nothing - it was never created in the first place

> --
> You received this message because you are subscribed to the Google
> Groups "Castle Project Users" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/castle-project-users/-/Ny6kvNH5doAJ.

mynkow

unread,
Jul 19, 2011, 6:20:02 PM7/19/11
to castle-pro...@googlegroups.com
it is created !!!

Krzysztof Koźmic

unread,
Jul 19, 2011, 6:25:03 PM7/19/11
to castle-pro...@googlegroups.com

Not in the code you pasted

On Jul 20, 2011 8:20 AM, "mynkow" <myn...@gmail.com> wrote:
> it is created !!!

>
> --
> You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/castle-project-users/-/-az2vnTi3rwJ.

mynkow

unread,
Jul 19, 2011, 6:38:30 PM7/19/11
to castle-pro...@googlegroups.com
yeah, I thought that the sample is OK. I will try another sample tomorrow.

mynkow

unread,
Jul 20, 2011, 4:37:54 AM7/20/11
to castle-pro...@googlegroups.com
Well, I am trying to reproduce this error for 2 hours now. At some point the error just disappeared. May be this is a local problem with VS. I am confused...
Reply all
Reply to author
Forward
0 new messages