---
title: "How to use Dependency Injection inside Hangfire Recurring Jobs in ASP.NET Core?"  
description: "How to use Dependency Injection inside Hangfire Recurring Jobs in ASP.NET Core?"  
author: "Hemant Patel"  
published: 2026-09-17  
updated: 2026-09-17  
canonical: https://www.mindstick.com/forum/162176/how-to-use-dependency-injection-inside-hangfire-recurring-jobs-in-asp-net-core  
category: "Hangfire"  
tags: ["hangfire", "dependency-injection", "recurring-jobs", "aspnet-core", "c-sharp"]  
reading_time: 1 minute  

---

# How to use Dependency Injection inside Hangfire Recurring Jobs in ASP.NET Core?

I am scheduling a daily recurring job in my ASP.NET Core application using Hangfire. My background task needs to access an Entity Framework Core DbContext to update database records.

## The Dilemma with Scoped Services

Since `DbContext` is registered as a scoped service, resolving it directly in a background job often leads to an **[ObjectDisposedException](https://www.mindstick.com/forum/2203/objectdisposedexception-while-using-include)** if the scope closes early.

### Example Job Code

```cs
// Class defining the job logic
public class EmailReportJob
{
    private readonly IApplicationDbContext _context;

    // Injecting scoped db context
    public EmailReportJob(IApplicationDbContext context)
    {
        _context = context;
    }

    // Method triggered by Hangfire
    public async Task ExecuteAsync()
    {
        // Perform operations on database
        var pending = await _context.Reports.Where(r => !r.IsSent).ToListAsync();
        // Send email logic here
    }
}
```

How does Hangfire handle service scope resolution when executing jobs? Does Hangfire create a new service scope for every job automatically, or do I need to explicitly create a scope using `IServiceScopeFactory` to avoid memory leaks and **scoped services** lifetime issues?


---

Original Source: https://www.mindstick.com/forum/162176/how-to-use-dependency-injection-inside-hangfire-recurring-jobs-in-asp-net-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
