dependency injection problem. hangfire jobs.
so i have this problem.
whenever i'm trying to inject dbcontext in my service layer for my hangfire job, i get error that db context is already being disposed.
but if i do by using IserviceScopeFactory. it's working. (found this solution in chatgpt).
but i don't rly like this.
how can i configure hangfire so it doesn't dispose dbcontext prematurely?
any ideas?
public class JobService : IJobService
{
private readonly IServiceScopeFactory _serviceScopeFactory;
public JobService(IServiceScopeFactory serviceScopeFactory)
{
_serviceScopeFactory = serviceScopeFactory;
}
public async void UpdateJobStatus()
{
using var scope = _serviceScopeFactory.CreateScope();
var jobRepository = scope.ServiceProvider.GetRequiredService<IJobRepository>();
}
}
whenever i'm trying to inject dbcontext in my service layer for my hangfire job, i get error that db context is already being disposed.
but if i do by using IserviceScopeFactory. it's working. (found this solution in chatgpt).
but i don't rly like this.
how can i configure hangfire so it doesn't dispose dbcontext prematurely?
any ideas?
public class JobService : IJobService
{
private readonly IServiceScopeFactory _serviceScopeFactory;
public JobService(IServiceScopeFactory serviceScopeFactory)
{
_serviceScopeFactory = serviceScopeFactory;
}
public async void UpdateJobStatus()
{
using var scope = _serviceScopeFactory.CreateScope();
var jobRepository = scope.ServiceProvider.GetRequiredService<IJobRepository>();
}
}