//////////// Program.cs ///////////////////////////
using Hangfire;
using Hangfire.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// adding your other services here
builder.Services.AddDbContextFactory<DBContext>(options => options.UseNpgsql("Connection_String"));
builder.Services.AddHangfire((serviceProvider, configuration) =>
configuration.UseEFCoreStorage(
() => serviceProvider.GetRequiredService<IDbContextFactory<DBContext>>().CreateDbContext(),
new EFCoreStorageOptions
{
CountersAggregationInterval = new TimeSpan(0, 5, 0),
DistributedLockTimeout = new TimeSpan(0, 10, 0),
JobExpirationCheckInterval = new TimeSpan(0, 30, 0),
QueuePollInterval = new TimeSpan(0, 0, 15),
Schema = string.Empty,
SlidingInvisibilityTimeout = new TimeSpan(0, 5, 0),
}));
builder.Services.AddHangfireServer(options => options.WorkerCount = 1);
// your other code of Program.cs
//////////// DBContext.cs ///////////////////////////////
using Hangfire.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace EFCore;
public class DBContext : DbContext
{
public DBContext(DbContextOptions<DBContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.OnHangfireModelCreating();
}
}