From 4926194b80153eef2ae4af9b557c459ee635bf11 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 27 Nov 2023 09:33:55 +0800 Subject: [PATCH 1/5] Add `OnActivated` event for `ServiceDescriptor`. Resolves #18241 --- .../AbpRegistrationBuilderExtensions.cs | 12 +++++- .../AutofacRegistration.cs | 7 +++- ...erviceCollectionLifetimeEventExtensions.cs | 31 ++++++++++++++ .../IOnServiceActivatedContext.cs | 6 +++ .../OnServiceActivatedContext.cs | 11 +++++ .../ServiceActivatedActionList.cs | 14 +++++++ .../ServiceCollectionRepositoryExtensions.cs | 12 ++---- .../Abp/Autofac/AutoFac_OnActivated_Tests.cs | 39 ++++++++++++++++++ .../RepositoryRegistration_Tests.cs | 40 +++++++++---------- 9 files changed, 140 insertions(+), 32 deletions(-) create mode 100644 framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionLifetimeEventExtensions.cs create mode 100644 framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IOnServiceActivatedContext.cs create mode 100644 framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/OnServiceActivatedContext.cs create mode 100644 framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ServiceActivatedActionList.cs create mode 100644 framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutoFac_OnActivated_Tests.cs diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs index 4ee9ba5916..61ea371a0c 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs @@ -15,7 +15,8 @@ public static class AbpRegistrationBuilderExtensions public static IRegistrationBuilder ConfigureAbpConventions( this IRegistrationBuilder registrationBuilder, IModuleContainer moduleContainer, - ServiceRegistrationActionList registrationActionList) + ServiceRegistrationActionList registrationActionList, + List> serviceActivatedActions) where TActivatorData : ReflectionActivatorData { var serviceType = registrationBuilder.RegistrationData.Services.OfType().FirstOrDefault()?.ServiceType; @@ -24,6 +25,15 @@ public static class AbpRegistrationBuilderExtensions return registrationBuilder; } + registrationBuilder.OnActivated(context => + { + var serviceActivatedContext = new OnServiceActivatedContext(context.Instance!); + foreach (var action in serviceActivatedActions) + { + action.Invoke(serviceActivatedContext); + } + }); + var implementationType = registrationBuilder.ActivatorData.ImplementationType; if (implementationType == null) { diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs b/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs index 56a5d7480c..afc528bb90 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs @@ -183,11 +183,14 @@ public static class AutofacRegistration { var moduleContainer = services.GetSingletonInstance(); var registrationActionList = services.GetRegistrationActionList(); + var activatedActionList = services.GetServiceActivatedActionList(); foreach (var descriptor in services) { if (descriptor.ImplementationType != null) { + var activatedActions = activatedActionList.GetActions(descriptor); + // Test if the an open generic type is being registered var serviceTypeInfo = descriptor.ServiceType.GetTypeInfo(); if (serviceTypeInfo.IsGenericTypeDefinition) @@ -196,7 +199,7 @@ public static class AutofacRegistration .RegisterGeneric(descriptor.ImplementationType) .As(descriptor.ServiceType) .ConfigureLifecycle(descriptor.Lifetime, lifetimeScopeTagForSingletons) - .ConfigureAbpConventions(moduleContainer, registrationActionList); + .ConfigureAbpConventions(moduleContainer, registrationActionList, activatedActions); } else { @@ -204,7 +207,7 @@ public static class AutofacRegistration .RegisterType(descriptor.ImplementationType) .As(descriptor.ServiceType) .ConfigureLifecycle(descriptor.Lifetime, lifetimeScopeTagForSingletons) - .ConfigureAbpConventions(moduleContainer, registrationActionList); + .ConfigureAbpConventions(moduleContainer, registrationActionList, activatedActions); } } else if (descriptor.ImplementationFactory != null) diff --git a/framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionLifetimeEventExtensions.cs b/framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionLifetimeEventExtensions.cs new file mode 100644 index 0000000000..14e2d28d51 --- /dev/null +++ b/framework/src/Volo.Abp.Core/Microsoft/Extensions/DependencyInjection/ServiceCollectionLifetimeEventExtensions.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using Volo.Abp.DependencyInjection; + +namespace Microsoft.Extensions.DependencyInjection; + +public static class ServiceCollectionLifetimeEventExtensions +{ + // OnActivated + public static void OnActivated(this IServiceCollection services, ServiceDescriptor descriptor, Action onActivatedAction) + { + GetOrCreateOnActivatedActionList(services).Add(new KeyValuePair>(descriptor, onActivatedAction)); + } + + public static ServiceActivatedActionList GetServiceActivatedActionList(this IServiceCollection services) + { + return GetOrCreateOnActivatedActionList(services); + } + + private static ServiceActivatedActionList GetOrCreateOnActivatedActionList(IServiceCollection services) + { + var actionList = services.GetSingletonInstanceOrNull>()?.Value; + if (actionList == null) + { + actionList = new ServiceActivatedActionList(); + services.AddObjectAccessor(actionList); + } + + return actionList; + } +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IOnServiceActivatedContext.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IOnServiceActivatedContext.cs new file mode 100644 index 0000000000..b2e1d10a93 --- /dev/null +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IOnServiceActivatedContext.cs @@ -0,0 +1,6 @@ +namespace Volo.Abp.DependencyInjection; + +public interface IOnServiceActivatedContext +{ + public object Instance { get; } +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/OnServiceActivatedContext.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/OnServiceActivatedContext.cs new file mode 100644 index 0000000000..91af1e130b --- /dev/null +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/OnServiceActivatedContext.cs @@ -0,0 +1,11 @@ +namespace Volo.Abp.DependencyInjection; + +public class OnServiceActivatedContext : IOnServiceActivatedContext +{ + public object Instance { get; set; } + + public OnServiceActivatedContext(object instance) + { + Instance = instance; + } +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ServiceActivatedActionList.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ServiceActivatedActionList.cs new file mode 100644 index 0000000000..3e3c4afd9e --- /dev/null +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ServiceActivatedActionList.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Extensions.DependencyInjection; + +namespace Volo.Abp.DependencyInjection; + +public class ServiceActivatedActionList : List>> +{ + public List> GetActions(ServiceDescriptor descriptor) + { + return this.Where(x => x.Key == descriptor).Select(x => x.Value).ToList(); + } +} diff --git a/framework/src/Volo.Abp.Ddd.Domain/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs b/framework/src/Volo.Abp.Ddd.Domain/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs index a877c673f1..6e98d442c5 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs @@ -84,22 +84,16 @@ public static class ServiceCollectionRepositoryExtensions bool replaceExisting, bool isReadOnlyRepository = false) { - ServiceDescriptor descriptor; + var descriptor = ServiceDescriptor.Transient(serviceType, implementationType); if (isReadOnlyRepository) { - services.TryAddTransient(implementationType); - descriptor = ServiceDescriptor.Transient(serviceType, provider => + services.OnActivated(descriptor, context => { - var repository = provider.GetRequiredService(implementationType); + var repository = context.Instance.As(); ObjectHelper.TrySetProperty(repository.As(), x => x.IsChangeTrackingEnabled, _ => false); - return repository; }); } - else - { - descriptor = ServiceDescriptor.Transient(serviceType, implementationType); - } if (replaceExisting) { diff --git a/framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutoFac_OnActivated_Tests.cs b/framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutoFac_OnActivated_Tests.cs new file mode 100644 index 0000000000..b2af6d85da --- /dev/null +++ b/framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutoFac_OnActivated_Tests.cs @@ -0,0 +1,39 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; +using Volo.Abp.Autofac.Interception; +using Xunit; + +namespace Volo.Abp.Autofac; + +public class AutoFac_OnActivated_Tests : Autofac_Interception_Test +{ + protected override Task AfterAddApplicationAsync(IServiceCollection services) + { + var serviceDescriptor = ServiceDescriptor.Transient(); + services.Add(serviceDescriptor); + services.OnActivated(serviceDescriptor, x => + { + x.Instance.As().Name += "1"; + }); + services.OnActivated(serviceDescriptor, x => + { + x.Instance.As().Name += "2"; + }); + + return base.AfterAddApplicationAsync(services); + } + + [Fact] + public void Should_Call_OnActivated() + { + var server = ServiceProvider.GetRequiredService(); + server.Name.ShouldBe("MyServer12"); + } +} + +class MyServer +{ + public string Name { get; set; } = "MyServer"; +} diff --git a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs index ca98017e0a..4a7a9fb28f 100644 --- a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs +++ b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs @@ -31,15 +31,15 @@ public class RepositoryRegistration_Tests //Assert //MyTestAggregateRootWithoutPk - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); //MyTestAggregateRootWithGuidPk - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); @@ -69,24 +69,24 @@ public class RepositoryRegistration_Tests //Assert //MyTestAggregateRootWithoutPk - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); //MyTestAggregateRootWithGuidPk - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); //MyTestEntityWithInt32Pk - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyBasicRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); + services.ShouldContainTransient(typeof(IReadOnlyBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); } @@ -114,20 +114,20 @@ public class RepositoryRegistration_Tests services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); //MyTestAggregateRootWithGuidPk - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyBasicRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); + services.ShouldContainTransient(typeof(IReadOnlyBasicRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); //MyTestEntityWithInt32Pk - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyBasicRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); + services.ShouldContainTransient(typeof(IReadOnlyBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); } @@ -209,10 +209,10 @@ public class RepositoryRegistration_Tests services.ShouldNotContainService(typeof(IRepository)); //MyTestAggregateRootWithGuidPk - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); } @@ -234,11 +234,11 @@ public class RepositoryRegistration_Tests new MyTestRepositoryRegistrar(options).AddRepositories(); //MyTestAggregateRootWithGuidPk - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyRepository)); - services.ShouldContainTransientImplementationFactory(typeof(IReadOnlyBasicRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); + services.ShouldContainTransient(typeof(IReadOnlyBasicRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); } From 3bfdcb2eb35ea729501cf65f80a1279098be171d Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 27 Nov 2023 09:48:27 +0800 Subject: [PATCH 2/5] Update `Dependency-Injection.md`. --- docs/en/Dependency-Injection.md | 18 ++++++++++++++++++ docs/zh-Hans/Dependency-Injection.md | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/docs/en/Dependency-Injection.md b/docs/en/Dependency-Injection.md index d8afe5c21c..143ec8cc19 100644 --- a/docs/en/Dependency-Injection.md +++ b/docs/en/Dependency-Injection.md @@ -480,6 +480,24 @@ This example simply checks if the service class has `MyLogAttribute` attribute a > Notice that `OnRegistered` callback might be called multiple times for the same service class if it exposes more than one service/interface. So, it's safe to use `Interceptors.TryAdd` method instead of `Interceptors.Add` method. See [the documentation](Dynamic-Proxying-Interceptors.md) of dynamic proxying / interceptors. +### IServiceCollection.OnActivated Event + +The `OnActivated` event is raised once a service is fully constructed. Here you can perform application-level tasks that depend on the service being fully constructed - these should be rare. + +````csharp +var serviceDescriptor = ServiceDescriptor.Transient(); +services.Add(serviceDescriptor); +if (setIsReadOnly) +{ + services.OnActivated(serviceDescriptor, x => + { + x.Instance.As().IsReadOnly = true; + }); +} +```` + +> Notice that `OnActivated` event can be registered multiple times for the same `ServiceDescriptor`. + ## 3rd-Party Providers While ABP has no core dependency to any 3rd-party DI provider, it's required to use a provider that supports dynamic proxying and some other advanced features to make some ABP features properly work. diff --git a/docs/zh-Hans/Dependency-Injection.md b/docs/zh-Hans/Dependency-Injection.md index 2c22c123ab..4cc6616f52 100644 --- a/docs/zh-Hans/Dependency-Injection.md +++ b/docs/zh-Hans/Dependency-Injection.md @@ -310,6 +310,24 @@ public class AppModule : AbpModule > 注意, 如果服务类公开了多于一个服务或接口, `OnRegistered` 回调(callback)可能被同一服务类多次调用. 因此, 较安全的方法是使用 `Interceptors.TryAdd` 方法而不是 `Interceptors.Add` 方法. 请参阅动态代理(dynamic proxying)/拦截器 [文档](Dynamic-Proxying-Interceptors.md). +### IServiceCollection.OnActivated 事件 + +一旦服务完全构建完成`OnActivated`事件就会触发. 你可以执行依赖于服务已完全构建的的一些任务, 虽然这种情况可能很少见. + +````csharp +var serviceDescriptor = ServiceDescriptor.Transient(); +services.Add(serviceDescriptor); +if (setIsReadOnly) +{ + services.OnActivated(serviceDescriptor, x => + { + x.Instance.As().IsReadOnly = true; + }); +} +```` + +> 注意,`OnActivated`事件可以为一个`ServiceDescriptor`注册多次. + ## 第三方提供程序 虽然ABP框架没有对任何第三方DI提供程序的核心依赖, 但它必须使用一个提供程序来支持动态代理(dynamic proxying)和一些高级特性以便ABP特性能正常工作. From 9496cfb86f37e729f193f91e19287cc1a20f8b87 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 27 Nov 2023 10:48:01 +0800 Subject: [PATCH 3/5] Add `InvokeActivatedActions` method. --- .../AbpRegistrationBuilderExtensions.cs | 33 +++++++++++++------ .../AutofacRegistration.cs | 6 ++-- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs index 61ea371a0c..73bcdd5702 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using Autofac.Core; using Autofac.Extras.DynamicProxy; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Autofac; using Volo.Abp.Castle.DynamicProxy; using Volo.Abp.DependencyInjection; @@ -14,26 +15,20 @@ public static class AbpRegistrationBuilderExtensions { public static IRegistrationBuilder ConfigureAbpConventions( this IRegistrationBuilder registrationBuilder, + ServiceDescriptor serviceDescriptor, IModuleContainer moduleContainer, ServiceRegistrationActionList registrationActionList, - List> serviceActivatedActions) + ServiceActivatedActionList serviceActivatedActions) where TActivatorData : ReflectionActivatorData { + registrationBuilder = registrationBuilder.InvokeActivatedActions(serviceActivatedActions, serviceDescriptor); + var serviceType = registrationBuilder.RegistrationData.Services.OfType().FirstOrDefault()?.ServiceType; if (serviceType == null) { return registrationBuilder; } - registrationBuilder.OnActivated(context => - { - var serviceActivatedContext = new OnServiceActivatedContext(context.Instance!); - foreach (var action in serviceActivatedActions) - { - action.Invoke(serviceActivatedContext); - } - }); - var implementationType = registrationBuilder.ActivatorData.ImplementationType; if (implementationType == null) { @@ -46,6 +41,24 @@ public static class AbpRegistrationBuilderExtensions return registrationBuilder; } + private static IRegistrationBuilder InvokeActivatedActions( + this IRegistrationBuilder registrationBuilder, + ServiceActivatedActionList serviceActivatedActions, + ServiceDescriptor serviceDescriptor) + where TActivatorData : ReflectionActivatorData + { + registrationBuilder.OnActivated(context => + { + var serviceActivatedContext = new OnServiceActivatedContext(context.Instance!); + foreach (var action in serviceActivatedActions.GetActions(serviceDescriptor)) + { + action.Invoke(serviceActivatedContext); + } + }); + + return registrationBuilder; + } + private static IRegistrationBuilder InvokeRegistrationActions( this IRegistrationBuilder registrationBuilder, ServiceRegistrationActionList registrationActionList, diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs b/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs index afc528bb90..9e86a0e3ce 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Extensions/DependencyInjection/AutofacRegistration.cs @@ -189,8 +189,6 @@ public static class AutofacRegistration { if (descriptor.ImplementationType != null) { - var activatedActions = activatedActionList.GetActions(descriptor); - // Test if the an open generic type is being registered var serviceTypeInfo = descriptor.ServiceType.GetTypeInfo(); if (serviceTypeInfo.IsGenericTypeDefinition) @@ -199,7 +197,7 @@ public static class AutofacRegistration .RegisterGeneric(descriptor.ImplementationType) .As(descriptor.ServiceType) .ConfigureLifecycle(descriptor.Lifetime, lifetimeScopeTagForSingletons) - .ConfigureAbpConventions(moduleContainer, registrationActionList, activatedActions); + .ConfigureAbpConventions(descriptor, moduleContainer, registrationActionList, activatedActionList); } else { @@ -207,7 +205,7 @@ public static class AutofacRegistration .RegisterType(descriptor.ImplementationType) .As(descriptor.ServiceType) .ConfigureLifecycle(descriptor.Lifetime, lifetimeScopeTagForSingletons) - .ConfigureAbpConventions(moduleContainer, registrationActionList, activatedActions); + .ConfigureAbpConventions(descriptor, moduleContainer, registrationActionList, activatedActionList); } } else if (descriptor.ImplementationFactory != null) From cc7e4627840022b2e818dac4c0bd1fb48934458b Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 27 Nov 2023 10:51:42 +0800 Subject: [PATCH 4/5] Update AbpRegistrationBuilderExtensions.cs --- .../Autofac/Builder/AbpRegistrationBuilderExtensions.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs index 73bcdd5702..18b6c3368d 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs @@ -18,10 +18,10 @@ public static class AbpRegistrationBuilderExtensions ServiceDescriptor serviceDescriptor, IModuleContainer moduleContainer, ServiceRegistrationActionList registrationActionList, - ServiceActivatedActionList serviceActivatedActions) + ServiceActivatedActionList serviceActivatedActionList) where TActivatorData : ReflectionActivatorData { - registrationBuilder = registrationBuilder.InvokeActivatedActions(serviceActivatedActions, serviceDescriptor); + registrationBuilder = registrationBuilder.InvokeActivatedActions(serviceActivatedActionList, serviceDescriptor); var serviceType = registrationBuilder.RegistrationData.Services.OfType().FirstOrDefault()?.ServiceType; if (serviceType == null) @@ -43,14 +43,14 @@ public static class AbpRegistrationBuilderExtensions private static IRegistrationBuilder InvokeActivatedActions( this IRegistrationBuilder registrationBuilder, - ServiceActivatedActionList serviceActivatedActions, + ServiceActivatedActionList serviceActivatedActionList, ServiceDescriptor serviceDescriptor) where TActivatorData : ReflectionActivatorData { registrationBuilder.OnActivated(context => { var serviceActivatedContext = new OnServiceActivatedContext(context.Instance!); - foreach (var action in serviceActivatedActions.GetActions(serviceDescriptor)) + foreach (var action in serviceActivatedActionList.GetActions(serviceDescriptor)) { action.Invoke(serviceActivatedContext); } From 145f2a8f7dd51484f73bde393de015b84d7c3a17 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 27 Nov 2023 11:07:58 +0800 Subject: [PATCH 5/5] Update AbpRegistrationBuilderExtensions.cs --- .../Autofac/Builder/AbpRegistrationBuilderExtensions.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs index 18b6c3368d..043f824b1a 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs @@ -18,10 +18,10 @@ public static class AbpRegistrationBuilderExtensions ServiceDescriptor serviceDescriptor, IModuleContainer moduleContainer, ServiceRegistrationActionList registrationActionList, - ServiceActivatedActionList serviceActivatedActionList) + ServiceActivatedActionList activatedActionList) where TActivatorData : ReflectionActivatorData { - registrationBuilder = registrationBuilder.InvokeActivatedActions(serviceActivatedActionList, serviceDescriptor); + registrationBuilder = registrationBuilder.InvokeActivatedActions(activatedActionList, serviceDescriptor); var serviceType = registrationBuilder.RegistrationData.Services.OfType().FirstOrDefault()?.ServiceType; if (serviceType == null) @@ -43,14 +43,14 @@ public static class AbpRegistrationBuilderExtensions private static IRegistrationBuilder InvokeActivatedActions( this IRegistrationBuilder registrationBuilder, - ServiceActivatedActionList serviceActivatedActionList, + ServiceActivatedActionList activatedActionList, ServiceDescriptor serviceDescriptor) where TActivatorData : ReflectionActivatorData { registrationBuilder.OnActivated(context => { var serviceActivatedContext = new OnServiceActivatedContext(context.Instance!); - foreach (var action in serviceActivatedActionList.GetActions(serviceDescriptor)) + foreach (var action in activatedActionList.GetActions(serviceDescriptor)) { action.Invoke(serviceActivatedContext); }