z4kn4fein / stashbox

A lightweight, fast, and portable dependency injection framework for .NET-based solutions.
https://z4kn4fein.github.io/stashbox
MIT License
141 stars 10 forks source link

Replace Existing doesn't working with Singleton Lifetime #98

Closed malibay81 closed 3 years ago

malibay81 commented 3 years ago

Hello,

it seems that ReplaceExisting doesn't work with SingletonLifetime. With TransientLifetime it's working like excepted.

I modified one of your test to reproduce this behaviour.

        [Fact]
        public void ReMapTests_Replace_SingleResolve_SingletonLifetime()
        {
            using var container = new StashboxContainer();

            container.Register<ITest, Test1>(context => context.WithName("test").WithSingletonLifetime());

            var test1 = container.Resolve<ITest>("test");

            Assert.IsType<Test1>(test1);

            container.Register<ITest, Test2>(context => context.WithName("test").ReplaceExisting().WithSingletonLifetime());

            var test2 = container.Resolve<ITest>("test");

            Assert.IsType<Test2>(test2);
        }

        interface ITest { }

        class Test1 : ITest
        {
        }

        class Test2 : ITest
        { }

Please, can you check this out !?

z4kn4fein commented 3 years ago

Hey, Thanks for reporting this issue! I'll look into it soon.

z4kn4fein commented 3 years ago

Hey, I fixed this and published it in the latest pre-release package, could you please try that it works for you as expected? Thanks!

malibay81 commented 3 years ago

Hey, thanks for the quick fix. Now it's working.