@@ -14,8 +14,8 @@ public class InMemoryOutboxConsumeContext :
1414 OutboxContext
1515 {
1616 readonly TaskCompletionSource < InMemoryOutboxConsumeContext > _clearToSend ;
17+ readonly InMemoryOutboxDeferredMethodCollection _deferredMethods ;
1718 readonly InMemoryOutboxMessageSchedulerContext _outboxSchedulerContext ;
18- readonly List < Func < Task > > _pendingActions ;
1919
2020 protected InMemoryOutboxConsumeContext ( ConsumeContext context )
2121 : base ( context )
@@ -27,9 +27,10 @@ protected InMemoryOutboxConsumeContext(ConsumeContext context)
2727 ReceiveContext = outboxReceiveContext ;
2828 PublishEndpointProvider = outboxReceiveContext . PublishEndpointProvider ;
2929
30- _pendingActions = [ ] ;
3130 _clearToSend = TaskUtil . GetTask < InMemoryOutboxConsumeContext > ( ) ;
3231
32+ _deferredMethods = new InMemoryOutboxDeferredMethodCollection ( _clearToSend . Task ) ;
33+
3334 if ( context . TryGetPayload ( out MessageSchedulerContext schedulerContext ) )
3435 {
3536 _outboxSchedulerContext = ( InMemoryOutboxMessageSchedulerContext ) context . AddOrUpdatePayload < MessageSchedulerContext > (
@@ -44,45 +45,14 @@ protected InMemoryOutboxConsumeContext(ConsumeContext context)
4445
4546 public Task Add ( Func < Task > method )
4647 {
47- if ( _clearToSend . Task . IsCompleted )
48- return method ( ) ;
49-
50- lock ( _pendingActions )
51- {
52- _pendingActions . Add ( method ) ;
53-
54- return Task . CompletedTask ;
55- }
48+ return _deferredMethods . Add ( method ) ;
5649 }
5750
5851 public virtual async Task ExecutePendingActions ( bool concurrentMessageDelivery )
5952 {
6053 _clearToSend . TrySetResult ( this ) ;
6154
62- Func < Task > [ ] pendingActions ;
63- lock ( _pendingActions )
64- pendingActions = _pendingActions . ToArray ( ) ;
65-
66- if ( pendingActions . Length > 0 )
67- {
68- if ( concurrentMessageDelivery )
69- {
70- var collection = new PendingTaskCollection ( pendingActions . Length ) ;
71-
72- collection . Add ( pendingActions . Select ( action => action ( ) ) ) ;
73-
74- await collection . Completed ( ) . ConfigureAwait ( false ) ;
75- }
76- else
77- {
78- foreach ( Func < Task > action in pendingActions )
79- {
80- var task = action ( ) ;
81- if ( task != null )
82- await task . ConfigureAwait ( false ) ;
83- }
84- }
85- }
55+ await _deferredMethods . Execute ( concurrentMessageDelivery ) . ConfigureAwait ( false ) ;
8656
8757 if ( _outboxSchedulerContext != null )
8858 {
@@ -99,8 +69,7 @@ public virtual async Task ExecutePendingActions(bool concurrentMessageDelivery)
9969
10070 public virtual async Task DiscardPendingActions ( )
10171 {
102- lock ( _pendingActions )
103- _pendingActions . Clear ( ) ;
72+ await _deferredMethods . Discard ( ) . ConfigureAwait ( false ) ;
10473
10574 if ( _outboxSchedulerContext != null )
10675 {
0 commit comments