Unity CreatChieldContainer

Apr 27, 2009 at 1:18 PM
Hi all,

I encounter a problem with the Unity CreatChiledContainer.
I register a type in a chield container as a singleton :

IUnityContainer chield = Container.CreateChieldContainer();

RegisterType<DataObject>(new ContainerControlledLifetimeManager());

When I'm trying to resolve the DataObject , I can't get it as singleton , I see the GetHashCode() of the returned object, It's a diferenet hash each time I call the Container.ResolveTypw<DataObject>();

Any help.


Apr 27, 2009 at 2:26 PM
Try RegisterInstance instead
Apr 27, 2009 at 7:50 PM
I doesn't work.
Very strange, When I register the type in the parent container it works as fine , but in the ChieldContainer  no success.

Apr 27, 2009 at 8:48 PM
When I tried to resolve the DataObject instanse from the Chield Container it works fine.

Previous retriving : Container.ResoveType<DataObject>();   //Returned not as singleton
New retriving : ChildContainer.ResolveType<DataObject>();  //It works.

Unity team , please help to explain this issue.

Apr 27, 2009 at 9:00 PM
Any chance you are using different instances of the child container to resolve your type?
Apr 27, 2009 at 9:24 PM
No, I checked this issue by the GetHashCode() I found the same instance of the chield is used.
Apr 27, 2009 at 9:52 PM
Could you please supply a small running sample or test case that demonstrates your issue? You've got a lot of confused syntax in your messages and it's getting hard to see what you're actually doing. Actual running code would really help.

Apr 27, 2009 at 10:37 PM
I don't find how to attach project code. Any way here is the code I wrote for simple WPF application.

/// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
        public IUnityContainer Container = new UnityContainer();

        //Not working staement
        public Person m_Person { get { return Container.Resolve<Person>(); } }
        //Working statment
        //public Person m_Person { get { return Container.Resolve<IUnityContainer>("Singletons").Resolve<Person>(); } }

        public Window1()

            Container.RegisterInstance<IUnityContainer>("Singletons", Container.CreateChildContainer(), new ContainerControlledLifetimeManager());
            Container.Resolve<IUnityContainer>("Singletons").RegisterType<Person>(new ContainerControlledLifetimeManager());

        private void Window_Loaded(object sender, RoutedEventArgs e)
            DataContext = m_Person;

        private void button1_Click(object sender, RoutedEventArgs e)
            MessageBox.Show(string.Format("First name: {0} , Last name: {1}",m_Person.FirstName,m_Person.LastName));

The binding system doesn't bind the text boxes in the form to the Person object properties, because the container return defferent instanse each time Binding occure. When I use the second statement every thing is fine.

Hope this help.
Apr 28, 2009 at 12:39 AM
If I understand your code sample correctly, you're creating a child container and configuring the Person object to have a singleton lifetime. You then resolve through the parent container, and it's not a singleton. That's exactly what's supposed to happen. You need to configure the lifetime in the container you're going to be resolving through. Setting it in the child container only affects resolves through that child container.

Am I understanding your code sample correctly? If so, why are you creating a child container at all? If not, please feel free to correct me.
Apr 28, 2009 at 7:57 AM
Thanks for your help,

1. I read in the documantations , that objects in chield container can be resolved from the parent, If I call to resolve an object , unity searches in chields containers then the patrent. I really did that because of saving the syntax to call the chield container then resolve the object.

2. I wanted to categorize all the objects I want them as singletons . In my app I need to destroy then and leave the other objects in the parent container.

Is there away to destory the singleton in the container.

If there is any better, it's my pleasure to know.

Apr 30, 2009 at 8:28 AM
Either the documentation is written backwards or you interpreted it backwards. Unity searches in child first, then parent, not the other way around. Sort of. It's complicated to explain, but generally works the way you expect. Or at least the way I expect. ;-)

"Destroying" singletons inside the container is done by calling Dispose() on the container. There's no way to get a specific one out without wiping the entire thing. Using a child container the way you've shown does exactly what you want, you just need to resolve through the child, not the parent.