Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not reuse ExternalComponentRegistration #961

Merged
merged 1 commit into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/Autofac/Core/Registration/ExternalRegistrySource.cs
Expand Up @@ -68,13 +68,9 @@ public IEnumerable<IComponentRegistration> 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))
Expand Down
26 changes: 26 additions & 0 deletions test/Autofac.Test/Core/Registration/ExternalRegistrySourceTests.cs
Expand Up @@ -36,5 +36,31 @@ public void OneTypeImplementTwoInterfaces_OtherObjectsImplementingOneOfThoseInte
var allImplementationsOfServiceA = lifetime.Resolve<IEnumerable<IServiceA>>();
Assert.Equal(2, allImplementationsOfServiceA.Count());
}

// Issue #960
[Fact]
public void TwoLayersOfExternalRegistration_OnDisposingInnerLayer_OuterLayerRemains()
{
var builder = new ContainerBuilder();
builder.RegisterType<ClassA>().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<ClassA>();

// Child has ExternalRegistration pointing upwards.
var child = middle.BeginLifetimeScope(cb => cb.Register(_ => new object()));
child.Resolve<ClassA>();

// 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<ClassA>();
}
}
}