Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 9adda02

Browse files
author
Ramón Tomás
committed
Updated OrderingIntegrationEvent svc
1 parent db35a5c commit 9adda02

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System;
1010
using System.Data.Common;
1111
using System.Diagnostics;
12+
using System.Linq;
1213
using System.Threading.Tasks;
1314

1415
namespace Ordering.API.Application.IntegrationEvents
@@ -35,15 +36,21 @@ public OrderingIntegrationEventService(IEventBus eventBus,
3536

3637
public async Task PublishThroughEventBusAsync(IntegrationEvent evt)
3738
{
38-
await SaveEventAndOrderingContextChangesAsync(evt);
39+
await SaveEventAsync(evt);
3940
_eventBus.Publish(evt);
4041
await _eventLogService.MarkEventAsPublishedAsync(evt);
4142
}
4243

43-
private async Task SaveEventAndOrderingContextChangesAsync(IntegrationEvent evt)
44+
private async Task SaveEventAsync(IntegrationEvent evt)
4445
{
45-
await _orderingContext.SaveChangesAsync();
46-
await _eventLogContext.SaveChangesAsync();
46+
var strategy = _orderingContext.Database.CreateExecutionStrategy();
47+
await strategy.ExecuteAsync(async () =>
48+
{
49+
await _orderingContext.BeginTransactionAsync();
50+
await _eventLogService.SaveEventAsync(evt, _orderingContext.GetCurrentTransaction.GetDbTransaction());
51+
await _orderingContext.CommitTransactionAsync();
52+
});
53+
4754
}
4855
}
4956
}

src/Services/Ordering/Ordering.Infrastructure/OrderingContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class OrderingContext : DbContext, IUnitOfWork
2929

3030
private OrderingContext(DbContextOptions<OrderingContext> options) : base (options) { }
3131

32+
public IDbContextTransaction GetCurrentTransaction => _currentTransaction;
33+
3234
public OrderingContext(DbContextOptions<OrderingContext> options, IMediator mediator) : base(options)
3335
{
3436
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));

0 commit comments

Comments
 (0)