diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs index b3c5d82c92..2031cb5558 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs @@ -67,11 +67,12 @@ public static class AbpRegistrationBuilderExtensions Type implementationType) where TActivatorData : ReflectionActivatorData { - //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()) + // Enable Property Injection only for types in an assembly containing an AbpModule and without a DisablePropertyInjection attribute on class or properties. + if (moduleContainer.Modules.Any(m => m.Assembly == implementationType.Assembly) && + implementationType.GetCustomAttributes(typeof(DisablePropertyInjectionAttribute), true).IsNullOrEmpty()) { - registrationBuilder = registrationBuilder.PropertiesAutowired(); + registrationBuilder = registrationBuilder.PropertiesAutowired((propertyInfo, _) => + propertyInfo.GetCustomAttributes(typeof(DisablePropertyInjectionAttribute), true).IsNullOrEmpty()); } return registrationBuilder; diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DisablePropertyInjectionAttribute.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DisablePropertyInjectionAttribute.cs index 0ce3148308..fad0d7334a 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DisablePropertyInjectionAttribute.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DisablePropertyInjectionAttribute.cs @@ -2,7 +2,8 @@ namespace Volo.Abp.DependencyInjection; -[AttributeUsage(AttributeTargets.Class)] +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)] 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 808af0aa87..52f07620fb 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 @@ -47,37 +47,36 @@ public abstract class DependencyInjection_Standard_Tests : AbpIntegratedTest().ProperyInjectedService.ShouldNotBeNull(); + GetRequiredService().PropertyInjectedService.ShouldNotBeNull(); } [Fact] public void Should_Inject_Services_As_Properties_For_Generic_Classes() { - GetRequiredService>().ProperyInjectedService.ShouldNotBeNull(); + GetRequiredService>().PropertyInjectedService.ShouldNotBeNull(); } [Fact] public void Should_Inject_Services_As_Properties_For_Generic_Concrete_Classes() { - GetRequiredService().ProperyInjectedService.ShouldNotBeNull(); + GetRequiredService().PropertyInjectedService.ShouldNotBeNull(); } [Fact] - public void Should_Not_Inject_Services_As_Properties_When_Disabled() + public void Should_Not_Inject_Services_As_Properties_When_Class_With_DisablePropertyInjection() { - GetRequiredService().ProperyInjectedService.ShouldBeNull(); + GetRequiredService().PropertyInjectedService.ShouldBeNull(); + GetRequiredService>().PropertyInjectedService.ShouldBeNull(); } [Fact] - public void Should_Not_Inject_Services_As_Properties_For_Generic_Classes_When_Disabled() + public void Should_Not_Inject_Services_As_Properties_When_Property_With_DisablePropertyInjection() { - GetRequiredService>().ProperyInjectedService.ShouldBeNull(); - } + GetRequiredService().PropertyInjectedService.ShouldNotBeNull(); + GetRequiredService().DisablePropertyInjectionService.ShouldBeNull(); - [Fact] - public void Should_Not_Inject_Services_As_Properties_For_Generic_Concrete_Classes_When_Disabled() - { - GetRequiredService().ProperyInjectedService.ShouldBeNull(); + GetRequiredService>().PropertyInjectedService.ShouldNotBeNull(); + GetRequiredService>().DisablePropertyInjectionService.ShouldBeNull(); } [Fact] @@ -163,18 +162,19 @@ public abstract class DependencyInjection_Standard_Tests : AbpIntegratedTest(); context.Services.AddType(); context.Services.AddTransient(typeof(GenericServiceWithPropertyInject<>)); - context.Services.AddTransient(typeof(GenericServiceWithPropertyInjectDisabled<>)); + context.Services.AddTransient(typeof(GenericServiceWithDisablePropertyInjectionOnClass<>)); + context.Services.AddTransient(typeof(GenericServiceWithDisablePropertyInjectionOnProperty<>)); } } public class ServiceWithPropertyInject : ITransientDependency { - public MyEmptyTransientService ProperyInjectedService { get; set; } + public MyEmptyTransientService PropertyInjectedService { get; set; } } public class GenericServiceWithPropertyInject : ITransientDependency { - public MyEmptyTransientService ProperyInjectedService { get; set; } + public MyEmptyTransientService PropertyInjectedService { get; set; } public T Value { get; set; } } @@ -185,21 +185,34 @@ public abstract class DependencyInjection_Standard_Tests : AbpIntegratedTest : ITransientDependency + public class GenericServiceWithDisablePropertyInjectionOnClass : ITransientDependency { - public MyEmptyTransientService ProperyInjectedService { get; set; } + public MyEmptyTransientService PropertyInjectedService { get; set; } public T Value { get; set; } } - public class ConcreteGenericServiceWithPropertyInjectDisabled : GenericServiceWithPropertyInjectDisabled + public class GenericServiceWithDisablePropertyInjectionOnProperty : ITransientDependency { + public MyEmptyTransientService PropertyInjectedService { get; set; } + [DisablePropertyInjection] + public MyEmptyTransientService DisablePropertyInjectionService { get; set; } + + public T Value { get; set; } } }