7
7
using Umbraco . Cms . Core . Notifications ;
8
8
using Umbraco . Cms . Core . Security ;
9
9
using Umbraco . Cms . Core . Services ;
10
+ using Umbraco . Cms . Core . Sync ;
10
11
using Umbraco . Cms . Infrastructure . Security ;
11
12
12
13
namespace Umbraco . Cms . Api . Delivery . Handlers ;
@@ -18,18 +19,24 @@ internal sealed class InitializeMemberApplicationNotificationHandler : INotifica
18
19
private readonly DeliveryApiSettings _deliveryApiSettings ;
19
20
private readonly IServiceScopeFactory _serviceScopeFactory ;
20
21
private readonly IMemberClientCredentialsManager _memberClientCredentialsManager ;
22
+ private readonly IServerRoleAccessor _serverRoleAccessor ;
23
+
24
+ private static readonly SemaphoreSlim _locker = new ( 1 ) ;
25
+ private static bool _isInitialized = false ;
21
26
22
27
public InitializeMemberApplicationNotificationHandler (
23
28
IRuntimeState runtimeState ,
24
29
IOptions < DeliveryApiSettings > deliveryApiSettings ,
25
30
ILogger < InitializeMemberApplicationNotificationHandler > logger ,
26
31
IServiceScopeFactory serviceScopeFactory ,
27
- IMemberClientCredentialsManager memberClientCredentialsManager )
32
+ IMemberClientCredentialsManager memberClientCredentialsManager ,
33
+ IServerRoleAccessor serverRoleAccessor )
28
34
{
29
35
_runtimeState = runtimeState ;
30
36
_logger = logger ;
31
37
_serviceScopeFactory = serviceScopeFactory ;
32
38
_memberClientCredentialsManager = memberClientCredentialsManager ;
39
+ _serverRoleAccessor = serverRoleAccessor ;
33
40
_deliveryApiSettings = deliveryApiSettings . Value ;
34
41
}
35
42
@@ -40,13 +47,34 @@ public async Task HandleAsync(UmbracoApplicationStartingNotification notificatio
40
47
return ;
41
48
}
42
49
43
- // we cannot inject the IMemberApplicationManager because it ultimately takes a dependency on the DbContext ... and during
44
- // install that is not allowed (no connection string means no DbContext)
45
- using IServiceScope scope = _serviceScopeFactory . CreateScope ( ) ;
46
- IMemberApplicationManager memberApplicationManager = scope . ServiceProvider . GetRequiredService < IMemberApplicationManager > ( ) ;
50
+ if ( _serverRoleAccessor . CurrentServerRole is ServerRole . Subscriber )
51
+ {
52
+ // subscriber instances should not alter the member application
53
+ return ;
54
+ }
55
+
56
+ try
57
+ {
58
+ await _locker . WaitAsync ( cancellationToken ) ;
59
+ if ( _isInitialized )
60
+ {
61
+ return ;
62
+ }
47
63
48
- await HandleMemberApplication ( memberApplicationManager , cancellationToken ) ;
49
- await HandleMemberClientCredentialsApplication ( memberApplicationManager , cancellationToken ) ;
64
+ // we cannot inject the IMemberApplicationManager because it ultimately takes a dependency on the DbContext ... and during
65
+ // install that is not allowed (no connection string means no DbContext)
66
+ using IServiceScope scope = _serviceScopeFactory . CreateScope ( ) ;
67
+ IMemberApplicationManager memberApplicationManager = scope . ServiceProvider . GetRequiredService < IMemberApplicationManager > ( ) ;
68
+
69
+ await HandleMemberApplication ( memberApplicationManager , cancellationToken ) ;
70
+ await HandleMemberClientCredentialsApplication ( memberApplicationManager , cancellationToken ) ;
71
+
72
+ _isInitialized = true ;
73
+ }
74
+ finally
75
+ {
76
+ _locker . Release ( ) ;
77
+ }
50
78
}
51
79
52
80
private async Task HandleMemberApplication ( IMemberApplicationManager memberApplicationManager , CancellationToken cancellationToken )
0 commit comments