From a56f3d3f1a92848d8cc6a7599dfe90c8b178a8f5 Mon Sep 17 00:00:00 2001 From: Alistair Evans Date: Mon, 27 Apr 2020 16:49:25 +0100 Subject: [PATCH 1/2] Fixes #1111, resolve one of the instance's services rather than the instance itself. --- global.json | 2 +- src/Autofac/RegistrationExtensions.cs | 4 ++- .../Lifetime/LifetimeEventTests.cs | 27 ++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/global.json b/global.json index 6fcca7f23..05d0ae3d2 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "3.1.200" + "version": "3.1.201" } } diff --git a/src/Autofac/RegistrationExtensions.cs b/src/Autofac/RegistrationExtensions.cs index b35449c1c..85eeb046f 100644 --- a/src/Autofac/RegistrationExtensions.cs +++ b/src/Autofac/RegistrationExtensions.cs @@ -101,12 +101,14 @@ public static IRegistrationBuilder( new AutoActivateService(), - new SimpleActivatorData(new DelegateActivator(typeof(T), (c, p) => c.Resolve())), + new SimpleActivatorData(new DelegateActivator(typeof(T), (c, p) => c.ResolveService(autoStartService))), new SingleRegistrationStyle()); RegistrationBuilder.RegisterSingleComponent(cr, activationRegistration); diff --git a/test/Autofac.Specification.Test/Lifetime/LifetimeEventTests.cs b/test/Autofac.Specification.Test/Lifetime/LifetimeEventTests.cs index 44ba6a9ea..d81203f3b 100644 --- a/test/Autofac.Specification.Test/Lifetime/LifetimeEventTests.cs +++ b/test/Autofac.Specification.Test/Lifetime/LifetimeEventTests.cs @@ -173,7 +173,28 @@ public void OnReleaseForSingletonStillFiresIfNotResolved() Assert.True(instance.Released); } - private class ReleasingClass + [Fact] + public void OnReleaseForSingletonAsInterfaceStillFiresIfNotResolved() + { + var builder = new ContainerBuilder(); + + var instance = new ReleasingClass(); + + builder.RegisterInstance(instance) + .As() + .OnRelease(s => s.Release()); + + using (var container = builder.Build()) + { + using (var scope = container.BeginLifetimeScope()) + { + } + } + + Assert.True(instance.Released); + } + + private class ReleasingClass : IReleasingService { public bool Released { get; set; } @@ -183,6 +204,10 @@ public void Release() } } + private interface IReleasingService + { + } + private class MethodInjection { public int Param { get; private set; } From ef8f45fda3375b7ec54630ebde5672003c2e26f2 Mon Sep 17 00:00:00 2001 From: Alistair Evans Date: Mon, 27 Apr 2020 16:50:46 +0100 Subject: [PATCH 2/2] Undo global.json change --- global.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global.json b/global.json index 05d0ae3d2..6fcca7f23 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "3.1.201" + "version": "3.1.200" } }