I’m trying to execute logic that will entail manipulating database records using Hangfire. I manage the transaction for this explicitly using a very basic implementation of a IUnitOfWork class that I have registered with the default resolver for ASPNET Core (I’m using MVC Core). However the
JobActivatorScope that is created internally to the
CoreBackgroundJobPerformer is created and subsequently disposed before I can do anything with it, regardless if the job was successful or failed.
After some digging I found that the
JobActivatorScope.Current will have an instance to this scope, but control of the execution before this scope is disposed, because of the
using statement, never gets handed outside of
CoreBackgroundJobPerformer.Perform(PerformContext context). Is there any way to try and execute code (to commit or rollback my transaction) before the
JobActivatorScope is disposed?
I’m currently using this exact pattern in an MVC 5 site, but I had implemented it by storing my own IoC scope inside of a ThreadStatic variable in a custom
JobActivator and a custom
JobFilter. Now that the scope is being implicitly created and used by Hangfire, I have much less control over how it’s used.