Open Closed

How to make Hangfire job creation part of transaction #10072


User avatar
0
sinancondo created

Currently if transaction is rolled back , the job create is not rolled back .

Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

2 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    1. Solution In ABP, background jobs are enqueued immediately by the configured IBackgroundJobManager (e.g., Hangfire), so if you enqueue inside a UnitOfWork and that UoW rolls back, the job will still exist unless you defer enqueueing until commit. Use one of these verified approaches to make job creation transactional:
    • Use UnitOfWork event to enqueue after successful commit
    public class MyAppService : ApplicationService
      {
          private readonly IBackgroundJobManager _backgroundJobManager;
    
          public MyAppService(IBackgroundJobManager backgroundJobManager)
          {
              _backgroundJobManager = backgroundJobManager;
          }
    
          [UnitOfWork]
          public virtual async Task DoSomethingAsync()
          {
              // 1) Perform your data changes here
              // await _repository.InsertAsync(entity);
    
              // 2) Register callback to run only if the UoW commits
              CurrentUnitOfWork.OnCompleted(async () =>
              {
                  await _backgroundJobManager.EnqueueAsync(
                      new MyJobArgs { /* ... */ }
                  );
              });
          }
      }
    
    • EnqueueAsync runs after the transaction has been committed. If the UoW rolls back, the callback isn’t executed, so no Hangfire job is created.

    • Use domain events + transactional outbox (for cross-context/microservices)

      • Publish a distributed event inside the same UoW as your data changes.
      • Enable ABP’s transactional outbox on your DbContext so the event is persisted in the same transaction and dispatched after commit.
      • Handle the event and enqueue the Hangfire job in the handler; if the transaction rolls back, the event is not dispatched, so no job is created.
      • This pattern ensures consistency and durability when you need cross-boundary processing.

    Notes:

    • Avoid calling _backgroundJobManager.EnqueueAsync directly before your data is committed.
    • The same pattern applies whether ABP Background Jobs are backed by Hangfire, RabbitMQ, or Quartz; the key is deferring the enqueue until commit.
    • For ABP v9.2 with EF Core + SQL Server, OnCompleted is the simplest approach when everything happens in the same UoW. Use the outbox if you need durable, cross-process delivery.
    1. Sources
    • Background Jobs (ABP) https://abp.io/docs/latest/framework/infrastructure/background-jobs
    • Background Workers (ABP) https://abp.io/docs/latest/framework/infrastructure/background-workers
    • Optimizing for Production: Background Jobs https://abp.io/docs/latest/deployment/optimizing-production

    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you share your job create code?

    And you can consider create job when the UOW is completed.

    YourUnitOfWork.OnCompleted(() =>
    {
        //create job
    });
    

    Thanks.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 16, 2026, 14:50
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.