kanayabhattad / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

Parent lifetime scopes not properly propagating registrations to child scopes #475

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi Guys,

This issue causes major problems for us - could someone please investigate.

It can be reproduced by the following unit test: please see my comment in the 
code for the line which doesn't produce expected result.

        public interface IServiceA { }
        public interface IServiceB { }
        public interface IServiceCommon { }

        public class ServiceA : IServiceA, IServiceCommon { }
        public class ServiceB1 : IServiceB, IServiceCommon { }
        public class ServiceB2 : IServiceB { }

        [Test]
        public void ServiceOverrideThroughIntermediateScopeIsCorrect()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType(typeof(ServiceA)).AsImplementedInterfaces().InstancePerLifetimeScope();
            builder.RegisterType(typeof(ServiceB1)).AsImplementedInterfaces().InstancePerLifetimeScope();

            var scope1 = builder.Build();

            {
                var serviceA = scope1.Resolve<IServiceA>();
                var serviceB = scope1.Resolve<IServiceB>();
                Assert.IsInstanceOf<ServiceA>(serviceA);
                Assert.IsInstanceOf<ServiceB1>(serviceB);
            }

            var scope2 = scope1.BeginLifetimeScope(cb => {
                cb.RegisterType(typeof(ServiceB2)).AsImplementedInterfaces().InstancePerLifetimeScope();
            });

            {
                var serviceA = scope2.Resolve<IServiceA>();
                var serviceB = scope2.Resolve<IServiceB>();
                Assert.IsInstanceOf<ServiceA>(serviceA);
                Assert.IsInstanceOf<ServiceB2>(serviceB);
            }

            var scope3 = scope2.BeginLifetimeScope(cb =>
            {
            });

            {
                var serviceA = scope3.Resolve<IServiceA>();
                var serviceB = scope3.Resolve<IServiceB>();
                Assert.IsInstanceOf<ServiceA>(serviceA);

                /* 
                    (IS) following line throws exception:
                    Expected: instance of <Autofac.Tests.Core.Lifetime.LifetimeScopeTests+ServiceB2> 
                    But was:  <Autofac.Tests.Core.Lifetime.LifetimeScopeTests+ServiceB1>
                */
                Assert.IsInstanceOf<ServiceB2>(serviceB);
            }
        }

It is reproduced on the latest version of Autofac - 3.1.1.

It would be much appreciated if someone could please pay urgent attention to it.

Regards

Iouri

Original issue reported on code.google.com by iou...@gmail.com on 2 Dec 2013 at 1:26

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 2 Dec 2013 at 11:40

GoogleCodeExporter commented 8 years ago
Moved issue to GitHub: https://github.com/autofac/Autofac/issues/475

Subsequent issue management will be held there; closing the issue on Google 
Code as "WontFix" because we will handle issue resolution on GitHub.

Original comment by travis.illig on 11 Feb 2014 at 12:02