diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs index 868ecb3fa5..b3c5d82c92 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs @@ -67,8 +67,9 @@ public static class AbpRegistrationBuilderExtensions Type implementationType) where TActivatorData : ReflectionActivatorData { - //Enable Property Injection only for types in an assembly containing an AbpModule - if (moduleContainer.Modules.Any(m => m.Assembly == implementationType.Assembly)) + //Enable Property Injection only for types in an assembly containing an AbpModule and without a DisablePropertyInjection attribute + if (moduleContainer.Modules.Any((IAbpModuleDescriptor m) => m.Assembly == implementationType.Assembly) + && !implementationType.GetCustomAttributes(typeof(DisablePropertyInjectionAttribute), true).Any()) { registrationBuilder = registrationBuilder.PropertiesAutowired(); } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DisablePropertyInjectionAttribute.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DisablePropertyInjectionAttribute.cs new file mode 100644 index 0000000000..0ce3148308 --- /dev/null +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DisablePropertyInjectionAttribute.cs @@ -0,0 +1,8 @@ +using System; + +namespace Volo.Abp.DependencyInjection; + +[AttributeUsage(AttributeTargets.Class)] +public class DisablePropertyInjectionAttribute : Attribute +{ +} diff --git a/framework/test/Volo.Abp.Core.Tests/Microsoft/Extensions/DependencyInjection/DependencyInjection_Tests.cs b/framework/test/Volo.Abp.Core.Tests/Microsoft/Extensions/DependencyInjection/DependencyInjection_Tests.cs index 5d77c6a00e..808af0aa87 100644 --- a/framework/test/Volo.Abp.Core.Tests/Microsoft/Extensions/DependencyInjection/DependencyInjection_Tests.cs +++ b/framework/test/Volo.Abp.Core.Tests/Microsoft/Extensions/DependencyInjection/DependencyInjection_Tests.cs @@ -62,6 +62,24 @@ public abstract class DependencyInjection_Standard_Tests : AbpIntegratedTest().ProperyInjectedService.ShouldNotBeNull(); } + [Fact] + public void Should_Not_Inject_Services_As_Properties_When_Disabled() + { + GetRequiredService().ProperyInjectedService.ShouldBeNull(); + } + + [Fact] + public void Should_Not_Inject_Services_As_Properties_For_Generic_Classes_When_Disabled() + { + GetRequiredService>().ProperyInjectedService.ShouldBeNull(); + } + + [Fact] + public void Should_Not_Inject_Services_As_Properties_For_Generic_Concrete_Classes_When_Disabled() + { + GetRequiredService().ProperyInjectedService.ShouldBeNull(); + } + [Fact] public void Singletons_Exposing_Multiple_Services_Should_Returns_The_Same_Instance() { @@ -145,7 +163,7 @@ public abstract class DependencyInjection_Standard_Tests : AbpIntegratedTest(); context.Services.AddType(); context.Services.AddTransient(typeof(GenericServiceWithPropertyInject<>)); - context.Services.AddTransient(typeof(ConcreteGenericServiceWithPropertyInject)); + context.Services.AddTransient(typeof(GenericServiceWithPropertyInjectDisabled<>)); } } @@ -165,4 +183,23 @@ public abstract class DependencyInjection_Standard_Tests : AbpIntegratedTest : ITransientDependency + { + public MyEmptyTransientService ProperyInjectedService { get; set; } + + public T Value { get; set; } + } + + public class ConcreteGenericServiceWithPropertyInjectDisabled : GenericServiceWithPropertyInjectDisabled + { + + } }