diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs index 61cee2ce90..5f5cf29827 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -136,6 +137,8 @@ namespace Volo.Abp } } + var assemblies = new HashSet(); + //ConfigureServices foreach (var module in Modules) { @@ -143,7 +146,12 @@ namespace Volo.Abp { if (!abpModule.SkipAutoServiceRegistration) { - Services.AddAssembly(module.Type.Assembly); + var assembly = module.Type.Assembly; + if (!assemblies.Contains(assembly)) + { + Services.AddAssembly(assembly); + assemblies.Add(assembly); + } } } diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DependencyInjection/AutoServiceRegistration_Tests.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DependencyInjection/AutoServiceRegistration_Tests.cs new file mode 100644 index 0000000000..648add30ca --- /dev/null +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DependencyInjection/AutoServiceRegistration_Tests.cs @@ -0,0 +1,41 @@ +using System.Linq; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; +using Volo.Abp.Modularity; +using Xunit; + +namespace Volo.Abp.DependencyInjection +{ + public class AutoServiceRegistration_Tests + { + [Fact] + public void AutoServiceRegistration_Should_Not_Duplicate_Test() + { + using (var application = AbpApplicationFactory.Create()) + { + //Act + application.Initialize(); + + //Assert + var services = application.ServiceProvider.GetServices().ToList(); + services.Count.ShouldBe(1); + } + } + } + + [DependsOn(typeof(TestDependedModule))] + public class TestModule : AbpModule + { + + } + + public class TestDependedModule : AbpModule + { + + } + + public class TestService : ITransientDependency + { + + } +}