We are evaluating Hangfire as outbox pattern in our application using Entity Framework Core.
In our business logic we want to save entities to our database and additionally enqueue a Hangfire background job. The enqueuing of the background job should be performed in the same database transaction with saving the entities to the database. (If saving entities to database fails, the job must not be enqueued.)
We are using entity framework’s database transactions:
using var transaction = await dataContext.Database.BeginTransactionAsync(cancellationToken);
Is it possible to re-use this transaction for enqueuing the hangfire job?
Note: We know that ambient transactions (System.Transactions.TransactionScope
) are supported. But we don’t use them since we also use Entity Framework’s connection resiliency (IExecutionStrategy
) too which is not supported with ambient transactions.