unitycontainer / interception

Unity.Interception package
Apache License 2.0
22 stars 17 forks source link

Bug: Registrations are wrong for .NET Core version. #30

Open hypercodeplace opened 5 years ago

hypercodeplace commented 5 years ago

Description

It's not the same as https://github.com/unitycontainer/container/issues/160, but perhaps it is related to each other.

How to reproduce

Compile and run the following code for .NET Core (I tried for 2.1) and Framework (tried for 4.7.2). The output is different.

class Program
{
    static void Main(string[] args)
    {
        var container = new UnityContainer()
            .AddNewExtension<Interception>()
            .RegisterType<Foo>()
            .RegisterType<IBar, Bar>();

        var child = container.CreateChildContainer()
            .RegisterType<IFoo, Foo>(new ContainerControlledLifetimeManager())
            .RegisterType<Foo>(new ContainerControlledLifetimeManager());

        var registrations = child.Registrations.Where(r => r.RegisteredType == typeof(Foo));

        foreach (var registration in registrations)
        {
            Console.WriteLine(
                $"{registration.RegisteredType.Name}->" +
                $"{registration.MappedToType.Name}" +
                $"[{registration.LifetimeManager}]");
        }
    }

    public interface IFoo { }
    public class Foo : IFoo { }

    public interface IBar { }
    public class Bar : Foo, IBar { }
}

For Framework version the output is

Foo->Foo[Lifetime:PerContainer]

But for .NET Core version is:

Foo->Foo[Lifetime:Transient] Foo->Foo[Lifetime:PerContainer]

Expected behavior

For .NET Core must be the same registration as for Framework. The transient registration is obviously wrong.