diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs index 3357d8f95c..e834b3d00d 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs @@ -38,7 +38,7 @@ public static class AbpRegistrationBuilderExtensions } registrationBuilder = registrationBuilder.EnablePropertyInjection(moduleContainer, implementationType); - registrationBuilder = registrationBuilder.InvokeRegistrationActions(registrationActionList, serviceType, implementationType); + registrationBuilder = registrationBuilder.InvokeRegistrationActions(registrationActionList, serviceType, implementationType, serviceDescriptor.ServiceKey); return registrationBuilder; } @@ -69,10 +69,11 @@ public static class AbpRegistrationBuilderExtensions this IRegistrationBuilder registrationBuilder, ServiceRegistrationActionList registrationActionList, Type serviceType, - Type implementationType) + Type implementationType, + object? serviceKey = null) where TActivatorData : ReflectionActivatorData { - var serviceRegistredArgs = new OnServiceRegistredContext(serviceType, implementationType); + var serviceRegistredArgs = new OnServiceRegistredContext(serviceType, implementationType, serviceKey); foreach (var registrationAction in registrationActionList) { diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IOnServiceRegistredContext.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IOnServiceRegistredContext.cs index 86dad068b6..a1c50c0c64 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IOnServiceRegistredContext.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IOnServiceRegistredContext.cs @@ -9,4 +9,6 @@ public interface IOnServiceRegistredContext ITypeList Interceptors { get; } Type ImplementationType { get; } + + object? ServiceKey { get; } } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/OnServiceRegistredContext.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/OnServiceRegistredContext.cs index a843b136f6..8491c94e91 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/OnServiceRegistredContext.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/OnServiceRegistredContext.cs @@ -13,10 +13,13 @@ public class OnServiceRegistredContext : IOnServiceRegistredContext public virtual Type ImplementationType { get; } - public OnServiceRegistredContext(Type serviceType, [NotNull] Type implementationType) + public virtual object? ServiceKey { get; } + + public OnServiceRegistredContext(Type serviceType, [NotNull] Type implementationType, object? serviceKey = null) { ServiceType = Check.NotNull(serviceType, nameof(serviceType)); ImplementationType = Check.NotNull(implementationType, nameof(implementationType)); + ServiceKey = serviceKey; Interceptors = new TypeList(); } 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 index b2af6d85da..46c9ea2e8c 100644 --- 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 @@ -31,9 +31,9 @@ public class AutoFac_OnActivated_Tests : Autofac_Interception_Test var server = ServiceProvider.GetRequiredService(); server.Name.ShouldBe("MyServer12"); } -} -class MyServer -{ - public string Name { get; set; } = "MyServer"; + class MyServer + { + public string Name { get; set; } = "MyServer"; + } } diff --git a/framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutoFac_OnRegistred_Tests.cs b/framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutoFac_OnRegistred_Tests.cs new file mode 100644 index 0000000000..34b739cb7f --- /dev/null +++ b/framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutoFac_OnRegistred_Tests.cs @@ -0,0 +1,30 @@ +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_OnRegistred_Tests : Autofac_Interception_Test +{ + protected override Task AfterAddApplicationAsync(IServiceCollection services) + { + services.Add(ServiceDescriptor.KeyedTransient("key")); + services.OnRegistered(onServiceRegistredContext => + { + if (onServiceRegistredContext.ImplementationType == typeof(MyServer)) + { + onServiceRegistredContext.ServiceKey.ShouldBe("key"); + } + }); + + return base.AfterAddApplicationAsync(services); + } + + class MyServer + { + public string Name { get; set; } = "MyServer"; + } +}