Skip to content

Commit 2b87579

Browse files
authored
Merge pull request #22744 from abpframework/OnRegistered
feat: add service key support in registration context.
2 parents 73c657a + 7a0b14a commit 2b87579

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> C
3838
}
3939

4040
registrationBuilder = registrationBuilder.EnablePropertyInjection(moduleContainer, implementationType);
41-
registrationBuilder = registrationBuilder.InvokeRegistrationActions(registrationActionList, serviceType, implementationType);
41+
registrationBuilder = registrationBuilder.InvokeRegistrationActions(registrationActionList, serviceType, implementationType, serviceDescriptor.ServiceKey);
4242

4343
return registrationBuilder;
4444
}
@@ -69,10 +69,11 @@ private static IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle>
6969
this IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> registrationBuilder,
7070
ServiceRegistrationActionList registrationActionList,
7171
Type serviceType,
72-
Type implementationType)
72+
Type implementationType,
73+
object? serviceKey = null)
7374
where TActivatorData : ReflectionActivatorData
7475
{
75-
var serviceRegistredArgs = new OnServiceRegistredContext(serviceType, implementationType);
76+
var serviceRegistredArgs = new OnServiceRegistredContext(serviceType, implementationType, serviceKey);
7677

7778
foreach (var registrationAction in registrationActionList)
7879
{

framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IOnServiceRegistredContext.cs

+2
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ public interface IOnServiceRegistredContext
99
ITypeList<IAbpInterceptor> Interceptors { get; }
1010

1111
Type ImplementationType { get; }
12+
13+
object? ServiceKey { get; }
1214
}

framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/OnServiceRegistredContext.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ public class OnServiceRegistredContext : IOnServiceRegistredContext
1313

1414
public virtual Type ImplementationType { get; }
1515

16-
public OnServiceRegistredContext(Type serviceType, [NotNull] Type implementationType)
16+
public virtual object? ServiceKey { get; }
17+
18+
public OnServiceRegistredContext(Type serviceType, [NotNull] Type implementationType, object? serviceKey = null)
1719
{
1820
ServiceType = Check.NotNull(serviceType, nameof(serviceType));
1921
ImplementationType = Check.NotNull(implementationType, nameof(implementationType));
22+
ServiceKey = serviceKey;
2023

2124
Interceptors = new TypeList<IAbpInterceptor>();
2225
}

framework/test/Volo.Abp.Autofac.Tests/Volo/Abp/Autofac/AutoFac_OnActivated_Tests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public void Should_Call_OnActivated()
3131
var server = ServiceProvider.GetRequiredService<MyServer>();
3232
server.Name.ShouldBe("MyServer12");
3333
}
34-
}
3534

36-
class MyServer
37-
{
38-
public string Name { get; set; } = "MyServer";
35+
class MyServer
36+
{
37+
public string Name { get; set; } = "MyServer";
38+
}
3939
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Shouldly;
5+
using Volo.Abp.Autofac.Interception;
6+
using Xunit;
7+
8+
namespace Volo.Abp.Autofac;
9+
10+
public class AutoFac_OnRegistred_Tests : Autofac_Interception_Test
11+
{
12+
protected override Task AfterAddApplicationAsync(IServiceCollection services)
13+
{
14+
services.Add(ServiceDescriptor.KeyedTransient<MyServer, MyServer>("key"));
15+
services.OnRegistered(onServiceRegistredContext =>
16+
{
17+
if (onServiceRegistredContext.ImplementationType == typeof(MyServer))
18+
{
19+
onServiceRegistredContext.ServiceKey.ShouldBe("key");
20+
}
21+
});
22+
23+
return base.AfterAddApplicationAsync(services);
24+
}
25+
26+
class MyServer
27+
{
28+
public string Name { get; set; } = "MyServer";
29+
}
30+
}

0 commit comments

Comments
 (0)