HangfireIO / HangfireIO/Hangfire

IServiceScopeFactory - for background task scoping.

Open
#1,472 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
C#
Stars
10.1k
Forks
1.8k
Avg merge
1h 19m
Merged PRs (30d)
1

Description

I am using entity framework core via dependency injection. I have noticed that after background tasks executing which utilize my DB context run for a period of time, i start getting some errors which i think relate to the connection not being closed properly - connection pool fills up?

I am trying to work out what is the best scoping to use for background tasks. Do i need to implement IServiceScopeFactory - something like this inside the background task. Or does hangfire already set the scope when executing background services.

```
using (var scope = _serviceScopeFactory.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService();
// now do your work
}

```
I guess it would be good to know the inner mechanics.

Contributor guide

Open the contributing guide

Research direction

Review how Hangfire scopes background jobs and how IServiceScopeFactory is used with an Entity Framework Core DbContext. Reproduce the reported long-running background-task errors and document the expected scope and connection behavior; done when the cause and recommended scoping are clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.