diff --git a/src/Autofac/Core/Registration/ExternalRegistrySource.cs b/src/Autofac/Core/Registration/ExternalRegistrySource.cs index 7ae5d37f0..64636d72f 100644 --- a/src/Autofac/Core/Registration/ExternalRegistrySource.cs +++ b/src/Autofac/Core/Registration/ExternalRegistrySource.cs @@ -68,13 +68,9 @@ public IEnumerable RegistrationsFor(Service service, Fun // Issue #272: Taking from the registry the following registrations: // - non-adapting own registrations: wrap them with ExternalComponentRegistration - // - if the registration is from the parent registry of this registry, - // it is already wrapped with ExternalComponentRegistration, - // share it as is 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)) diff --git a/test/Autofac.Test/Core/Registration/ExternalRegistrySourceTests.cs b/test/Autofac.Test/Core/Registration/ExternalRegistrySourceTests.cs index d1091f8a1..d1eabadef 100644 --- a/test/Autofac.Test/Core/Registration/ExternalRegistrySourceTests.cs +++ b/test/Autofac.Test/Core/Registration/ExternalRegistrySourceTests.cs @@ -36,5 +36,31 @@ public void OneTypeImplementTwoInterfaces_OtherObjectsImplementingOneOfThoseInte var allImplementationsOfServiceA = lifetime.Resolve>(); Assert.Equal(2, allImplementationsOfServiceA.Count()); } + + // Issue #960 + [Fact] + public void TwoLayersOfExternalRegistration_OnDisposingInnerLayer_OuterLayerRemains() + { + var builder = new ContainerBuilder(); + builder.RegisterType().InstancePerLifetimeScope(); + + // Root has the main registration. + var root = builder.Build(); + + // Middle has ExternalRegistration pointing upwards. + var middle = root.BeginLifetimeScope(cb => cb.Register(_ => new object())); + middle.Resolve(); + + // Child has ExternalRegistration pointing upwards. + var child = middle.BeginLifetimeScope(cb => cb.Register(_ => new object())); + child.Resolve(); + + // This should only dispose the registration in child, not the one in middle. + child.Dispose(); + + // We check by trying to use the registration in middle. + // If too much got disposed, this will throw ObjectDisposedException. + middle.Resolve(); + } } }