Skip to content

Singleton open generic type's resolve fails while resolving from child containers #210

@gedbac

Description

@gedbac

Description

I have created unit container, registered open type as singleton and I am trying to resolve them in child containers and I got error: "System.InvalidOperationException: 'ContainerControlledLifetimeManager can only be set once'". I am expecting resolve of singleton services on different child containers will return same instance.

To Reproduce

Sample is bellow:

        [Test]
        public void Test()
        {
            IUnityContainer uc = new UnityContainer();

            uc.RegisterSingleton(typeof(IBoo<>), typeof(Boo<>));

            IBoo<object> boo1 = null;
            IBoo<object> boo2 = null;

            using (var scope = uc.CreateChildContainer())
            {
                boo1 = scope.Resolve<IBoo<object>>();
            }

            using (var scope = uc.CreateChildContainer())
            {
                boo2 = scope.Resolve<IBoo<object>>(); // Throws => System.InvalidOperationException: 'ContainerControlledLifetimeManager can only be set once'
            }

            NotNull(boo1);
            NotNull(boo2);
            AreEqual(boo1, boo2);
        }

    public interface IBoo<T>
    {

    }

    public class Boo<T> : IBoo<T>
    {

    }

Metadata

Metadata

Assignees

Labels

masterIssue has been merged into ‘master’releaseRelease branch

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions