From a9a5d1c3248588d6eb15c950246f12ee9a8d408c Mon Sep 17 00:00:00 2001 From: Chen Y Machluf Date: Thu, 14 Feb 2019 13:42:13 +0200 Subject: [PATCH] fix https://github.com/autofac/Autofac/issues/960 --- .../Registration/ExternalRegistrySource.cs | 8 --- .../Core/Lifetime/LifetimeScopeTests.cs | 55 +++++++++++++++++++ 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/Autofac/Core/Registration/ExternalRegistrySource.cs b/src/Autofac/Core/Registration/ExternalRegistrySource.cs index 7ae5d37f0..5471a09fe 100644 --- a/src/Autofac/Core/Registration/ExternalRegistrySource.cs +++ b/src/Autofac/Core/Registration/ExternalRegistrySource.cs @@ -74,14 +74,6 @@ public IEnumerable RegistrationsFor(Service service, Fun return _registry.RegistrationsFor(service) .Where(r => r is ExternalComponentRegistration || !r.IsAdapting()) .Select(r => - r as ExternalComponentRegistration ?? - - // equivalent to following registation builder - // RegistrationBuilder.ForDelegate(r.Activator.LimitType, (c, p) => c.ResolveComponent(r, p)) - // .Targeting(r) - // .As(service) - // .ExternallyOwned() - // .CreateRegistration() new ExternalComponentRegistration( Guid.NewGuid(), new DelegateActivator(r.Activator.LimitType, (c, p) => c.ResolveComponent(r, p)), diff --git a/test/Autofac.Test/Core/Lifetime/LifetimeScopeTests.cs b/test/Autofac.Test/Core/Lifetime/LifetimeScopeTests.cs index c0774588c..e4e416b87 100644 --- a/test/Autofac.Test/Core/Lifetime/LifetimeScopeTests.cs +++ b/test/Autofac.Test/Core/Lifetime/LifetimeScopeTests.cs @@ -581,5 +581,60 @@ public void BeginLifetimeScopeCannotBeCalledWithDuplicateTag() Assert.Equal(expectedMessage, exception.Message); } + + //issue 690 + [Fact] + public void ParentRegistrationDidntDisposeByChildScope() + { + var builder = new ContainerBuilder(); + builder.RegisterType().As().SingleInstance(); + builder.Register(x => x.Resolve().CreateCService()); + builder.RegisterType(); + + var container = builder.Build(); + + using (var moduleScope = container.BeginLifetimeScope(x => + { + x.RegisterType().AsSelf().InstancePerLifetimeScope(); + })) + { + var stub = moduleScope.Resolve(); + using (var syncScope = moduleScope.BeginLifetimeScope(y => y.RegisterInstance(stub).AsSelf().ExternallyOwned())) + { + syncScope.Resolve(); + } + + stub = moduleScope.Resolve(); + } + } + + interface IServiceCFactory + { + ServiceC CreateCService(); + } + + class ServiceC + { + } + + class ServiceD + { + public ServiceD(ServiceC serviceC) { } + } + + class ServiceE + { + public ServiceE(ServiceC serviceC) { } + } + + class ServiceCFactory : IServiceCFactory + { + public ServiceC CreateCService() + { + return new ServiceC(); + } + } + + } }