Edition 0 Updated to asp. Net core 0



Yüklə 11,82 Mb.
Pdf görüntüsü
səhifə254/288
tarix12.07.2023
ölçüsü11,82 Mb.
#136458
1   ...   250   251   252   253   254   255   256   257   ...   288
NET-Microservices-Architecture-for-Containerized-NET-Applications

public
class
CatalogIntegrationEventService : ICatalogIntegrationEventService 

//…
public
async Task 
SaveEventAndCatalogContextChangesAsync

IntegrationEvent evt) 

// Use of an EF Core resiliency strategy when using multiple DbContexts
// within an explicit BeginTransaction():
// https://learn.microsoft.com/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 _eventLogService.
SaveEventAsync
(evt, 
_catalogContext.
Database
.
CurrentTransaction
.
GetDbTransaction
()); 
}); 


The 
ResilientTransaction.ExecuteAsync
method basically begins a transaction from the passed 
DbContext
(
_catalogContext
) and then makes the 
EventLogService
use that transaction to save 
changes from the 
IntegrationEventLogContext
and then commits the whole transaction. 
public
class
ResilientTransaction 

private
DbContext _context; 
private
ResilientTransaction
(DbContext context) => 
_context = context ?? 
throw
new
ArgumentNullException
(
nameof
(context)); 
public
static
ResilientTransaction 
New
(DbContext context) => 
new
ResilientTransaction
(context); 
public
async Task 
ExecuteAsync
(Func action) 



299 
CHAPTER 7 | Implement resilient applications 
// Use of an EF Core resiliency strategy when using multiple DbContexts
// within an explicit BeginTransaction():
// https://learn.microsoft.com/ef/core/miscellaneous/connection-resiliency
var
strategy = _context.
Database
.
CreateExecutionStrategy
(); 
await strategy.
ExecuteAsync
(
async
() => 

await 
using
var
transaction = await _context.
Database
.
BeginTransactionAsync
(); 
await 
action
(); 
await transaction.
CommitAsync
(); 
}); 



Yüklə 11,82 Mb.

Dostları ilə paylaş:
1   ...   250   251   252   253   254   255   256   257   ...   288




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin