Hangfire.EntityFrameworkCore icon indicating copy to clipboard operation
Hangfire.EntityFrameworkCore copied to clipboard

Cannot use a resilience policy on a DbContext due to ExpirationManager explicitly starting a Transaction

Open davidscheidt2 opened this issue 1 year ago • 0 comments

Hi, My code is using the PostgreSQL EnableRetryOnFailure with the DbContext that is passed to the Hangfire.io library. Unfortunately, the ExpirationManager uses BeginTransaction before the SaveChanges is causing the following exception:

System.InvalidOperationException: The configured execution strategy 'NpgsqlRetryingExecutionStrategy' does not support user-initiated transactions. Use the execution strategy returned by 'DbContext.Database.CreateExecutionStrategy()' to execute all the operations in the transaction as a retriable unit. at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.OnFirstExecution() at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded) at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess) at Hangfire.EntityFrameworkCore.ExpirationManager.<>c.<RemoveExpiredJobs>b__6_1(DbContext context) at Hangfire.EntityFrameworkCore.EFCoreStorage.UseContext[T](Func2 func) at Hangfire.EntityFrameworkCore.ExpirationManager.<RemoveExpiredJobs>b__6_0() at Hangfire.EntityFrameworkCore.ExpirationManager.UseLock(Action action) at Hangfire.EntityFrameworkCore.ExpirationManager.RemoveExpiredJobs() at Hangfire.EntityFrameworkCore.ExpirationManager.Execute(CancellationToken cancellationToken) at Hangfire.Processing.BackgroundExecution.Run(Action2 callback, Object state) in C:\projects\hangfire-525\src\Hangfire.Core\Processing\BackgroundExecution.cs:line 118

Should this be able to be a flag to enable / disable transactions, or check to see if the DbContext will support a transaction?

davidscheidt2 avatar Feb 26 '24 16:02 davidscheidt2