Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to resolve from ChildContainer when using singleton #317

Open
Josch600 opened this issue Jun 3, 2021 · 1 comment
Open

Unable to resolve from ChildContainer when using singleton #317

Josch600 opened this issue Jun 3, 2021 · 1 comment
Assignees
Labels
Bug 🐛 Requires Investigation 👮 Requires further verification

Comments

@Josch600
Copy link

Josch600 commented Jun 3, 2021

We ran into a problem using Unity.Container 5.11.11 and Unity.Abstraction 5.11.7, which I could isolate to the follwing code:

namespace UnitTests
{
    using FluentAssertions;
    using Unity;
    using Unity.Lifetime;
    using Xunit;


    public interface Interface1 { }
    public interface Interface2 { }
    public interface Interface3 { }

    public class Class1 : Interface1 { }

    public class Class2 : Interface2
    {
        public Class2(Interface1 param) { }
    }

    public class Class3 : Interface3
    {
        public Class3(Interface2 param) { }
    }

    public class UnitTest1
    {
        [Fact]
        public void Resolve_Interface3FromMainContainer_GetsClass3()
        {
            var unityContainer = new UnityContainer();

            unityContainer.RegisterType<Interface1, Class1>();
            unityContainer.RegisterType<Interface2, Class2>(new SingletonLifetimeManager());
            unityContainer.RegisterType<Interface3, Class3>();

            // works fine
            unityContainer.Resolve<Interface3>().Should().BeOfType<Class3>();
        }

        [Fact]
        public void Resolve_Interface3FromChildContainer_GetsClass3()
        {
            IUnityContainer mainContainer = new UnityContainer();
            var unityContainer = mainContainer.CreateChildContainer();

            unityContainer.RegisterType<Interface1, Class1>();
            unityContainer.RegisterType<Interface2, Class2>(new SingletonLifetimeManager());
            unityContainer.RegisterType<Interface3, Class3>();

            // resolve crashes with "Resolution failed with error: No public constructor is available for type UnitTests.Interface1."
            unityContainer.Resolve<Interface3>().Should().BeOfType<Class3>();
        }
    }
}

Everything runs fine when using a directly created container (see first test) and goes wrong when using a child container (second test). Is this the intended behaviour, maybe because of the singleton or is it a bug?

@ENikS ENikS transferred this issue from unitycontainer/unity Jun 3, 2021
@ENikS ENikS self-assigned this Jun 3, 2021
@ENikS ENikS added Bug 🐛 Requires Investigation 👮 Requires further verification labels Jun 3, 2021
@ENikS
Copy link
Contributor

ENikS commented Oct 31, 2021

The development and support for this project is on pause until financial situation is improved
unitycontainer/unity#368

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 Requires Investigation 👮 Requires further verification
Projects
None yet
Development

No branches or pull requests

2 participants