Repository navigation
Expand file tree
/
Copy pathCatalogIntegrationEventService.cs
More file actions
60 lines (52 loc) · 2.25 KB
/
Copy pathCatalogIntegrationEventService.cs
File metadata and controls
60 lines (52 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
namespace eShop.Catalog.API.IntegrationEvents;
public sealed class CatalogIntegrationEventService(ILogger<CatalogIntegrationEventService> logger,
IEventBus eventBus,
CatalogContext catalogContext,
IIntegrationEventLogService integrationEventLogService)
: ICatalogIntegrationEventService, IDisposable
{
private volatile bool disposedValue;
public async Task PublishThroughEventBusAsync(IntegrationEvent evt)
{
try
{
logger.LogInformation("Publishing integration event: {IntegrationEventId_published} - ({@IntegrationEvent})", evt.Id, evt);
await integrationEventLogService.MarkEventAsInProgressAsync(evt.Id);
await eventBus.PublishAsync(evt);
await integrationEventLogService.MarkEventAsPublishedAsync(evt.Id);
}
catch (Exception ex)
{
logger.LogError(ex, "Error Publishing integration event: {IntegrationEventId} - ({@IntegrationEvent})", evt.Id, evt);
await integrationEventLogService.MarkEventAsFailedAsync(evt.Id);
}
}
public async Task SaveEventAndCatalogContextChangesAsync(IntegrationEvent evt)
{
logger.LogInformation("CatalogIntegrationEventService - Saving changes and integrationEvent: {IntegrationEventId}", evt.Id);
//Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
//See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
await ResilientTransaction.New(catalogContext).ExecuteAsync(async () =>
{
// Achieving atomicity between original catalog database operation and the IntegrationEventLog thanks to a local transaction
await catalogContext.SaveChangesAsync();
await integrationEventLogService.SaveEventAsync(evt, catalogContext.Database.CurrentTransaction);
});
}
private void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
(integrationEventLogService as IDisposable)?.Dispose();
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}