Skip to content

Commit fb6c31b

Browse files
committed
Fix a bug in the original #1102 fix, which incorrectly relied on the target component's first service being unique to that component
1 parent e398992 commit fb6c31b

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/Autofac/RegistrationExtensions.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public static IRegistrationBuilder<T, SimpleActivatorData, SingleRegistrationSty
6969

7070
rb.SingleInstance();
7171

72+
// https://github.com/autofac/Autofac/issues/1102
73+
// Single instance registrations with any custom activation phases (i.e. activation handlers) need to be auto-activated,
74+
// so that other behavior (such as OnRelease) that expects 'normal' object lifetime behavior works as expected.
75+
rb.RegistrationData.AddService(new AutoActivateService());
76+
7277
rb.RegistrationData.DeferredCallback = builder.RegisterCallback(cr =>
7378
{
7479
if (rb.RegistrationData.Lifetime is not RootScopeLifetime ||
@@ -79,21 +84,6 @@ public static IRegistrationBuilder<T, SimpleActivatorData, SingleRegistrationSty
7984

8085
activator.DisposeInstance = rb.RegistrationData.Ownership == InstanceOwnership.OwnedByLifetimeScope;
8186

82-
// https://github.com/autofac/Autofac/issues/1102
83-
// Single instance registrations with any custom activation phases (i.e. activation handlers) need to be auto-activated,
84-
// so that other behavior (such as OnRelease) that expects 'normal' object lifetime behavior works as expected.
85-
if (rb.ResolvePipeline.Middleware.Any(s => s.Phase == PipelinePhase.Activation))
86-
{
87-
var autoStartService = rb.RegistrationData.Services.First();
88-
89-
var activationRegistration = new RegistrationBuilder<T, SimpleActivatorData, SingleRegistrationStyle>(
90-
new AutoActivateService(),
91-
new SimpleActivatorData(new DelegateActivator(typeof(T), (c, p) => c.ResolveService(autoStartService))),
92-
new SingleRegistrationStyle());
93-
94-
RegistrationBuilder.RegisterSingleComponent(cr, activationRegistration);
95-
}
96-
9787
RegistrationBuilder.RegisterSingleComponent(cr, rb);
9888
});
9989

test/Autofac.Specification.Test/Features/StartableTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ public void AutoActivate_DoesNotHideDefaultSelfService()
177177
Assert.True(container.IsRegistered<MyComponent2>());
178178
}
179179

180+
[Fact]
181+
public void AutoActivate_RegisterInstanceActivationWorksWhenDefaultServiceOverloaded()
182+
{
183+
var instanceCount = 0;
184+
var builder = new ContainerBuilder();
185+
builder.RegisterInstance(new MyComponent2()).As<object>().OnActivated(_ => instanceCount++);
186+
builder.RegisterType<object>();
187+
builder.Build();
188+
Assert.Equal(1, instanceCount);
189+
}
190+
180191
private class ComponentTakesStartableDependency : IStartable
181192
{
182193
public ComponentTakesStartableDependency(StartableTakesDependency dependency, bool expectStarted)

0 commit comments

Comments
 (0)