From faad6f8da22a3f2740829ef8d2d96c3d71883f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 25 Jun 2019 12:01:27 +0300 Subject: [PATCH] Fixes #1367: Actions subscribed to ServiceCollectionRegistrationActionExtensions.OnRegistred are not invoked --- .../ServiceCollectionExtensions.cs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/ServiceCollectionExtensions.cs b/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/ServiceCollectionExtensions.cs index 1195cb9fb1..4b61ae5a6e 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/ServiceCollectionExtensions.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/ServiceCollectionExtensions.cs @@ -24,7 +24,11 @@ // OTHER DEALINGS IN THE SOFTWARE. using System; +using System.Collections.Generic; +using System.Linq; +using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; namespace Autofac.Extensions.DependencyInjection { @@ -41,7 +45,23 @@ namespace Autofac.Extensions.DependencyInjection /// The service collection. public static IServiceCollection AddAutofac(this IServiceCollection services, Action configurationAction = null) { - return services.AddSingleton>(new AutofacServiceProviderFactory(configurationAction)); + return services + .ClearServiceProviderFactories() + .AddSingleton>(new AutofacServiceProviderFactory(configurationAction)); + } + + private static IServiceCollection ClearServiceProviderFactories([NotNull] this IServiceCollection services) + { + Check.NotNull(services, nameof(services)); + + services.RemoveAll( + service => service.ImplementationInstance? + .GetType() + .GetInterfaces() + .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IServiceProviderFactory<>)) == true + ); + + return services; } } }