Browse Source
Merge pull request #22744 from abpframework/OnRegistered
feat: add service key support in registration context.
pull/22776/head
Engincan VESKE
10 months ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with
44 additions and
8 deletions
-
framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs
-
framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IOnServiceRegistredContext.cs
-
framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/OnServiceRegistredContext.cs
-
framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutoFac_OnActivated_Tests.cs
-
framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutoFac_OnRegistred_Tests.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<TLimit, TActivatorData, TRegistrationStyle> 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) |
|
|
|
{ |
|
|
|
|
|
|
|
@ -9,4 +9,6 @@ public interface IOnServiceRegistredContext |
|
|
|
ITypeList<IAbpInterceptor> Interceptors { get; } |
|
|
|
|
|
|
|
Type ImplementationType { get; } |
|
|
|
|
|
|
|
object? ServiceKey { get; } |
|
|
|
} |
|
|
|
|
|
|
|
@ -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<IAbpInterceptor>(); |
|
|
|
} |
|
|
|
|
|
|
|
@ -31,9 +31,9 @@ public class AutoFac_OnActivated_Tests : Autofac_Interception_Test |
|
|
|
var server = ServiceProvider.GetRequiredService<MyServer>(); |
|
|
|
server.Name.ShouldBe("MyServer12"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
class MyServer |
|
|
|
{ |
|
|
|
public string Name { get; set; } = "MyServer"; |
|
|
|
class MyServer |
|
|
|
{ |
|
|
|
public string Name { get; set; } = "MyServer"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -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<MyServer, MyServer>("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"; |
|
|
|
} |
|
|
|
} |