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; } } }