An Entity Framework Core provider-neutral job storage implementation for Hangfire developed by Sergey Odinokov.
To install Hangfire Entity Framework Core Storage, run the following command in the Nuget Package Manager Console:
PM> Install-Package Hangfire.EntityFrameworkCore
After installation, update your existing OWIN Startup with the following lines of code:
public void Configuration(IAppBuilder app)
{
// Register Entity Framework Core Storage
GlobalConfiguration.Configuration.UseEFCoreStorage(
// Configure Entity Framework Core to connect database, e.g. SQL Server
builder => builder.UseSqlServer("Data Source=(LocalDB)\\MSSQLLocalDB;Database=Hangfire"),
// Optionally configure Entity Framework Core Storage
new EFCoreStorageOptions()).
// Optionally register database creator
UseDatabaseCreator();
// Configure Hangfire Server and/or Hangfire Dashboard
app.UseHangfireServer();
app.UseHangfireDashboard();
}
There is an example to use Hangfire.EntityFrameworkCore with ASP.NET Core.
Currently, automatic migrations are not implemented. The migrations support planned and will be implemented on future releases.
As of the 0.3.0
version you have the ability to attach the tables required for this library to your own DbContext. Since the tables are attached to your own DbContext this means that the migrations are also attached to this DbContext and managed by the regular dotnet ef
migration flow.
There is an example of this configuration found here, however the important sections are listed below.
In Program.cs
:
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("HangfireConnection")
?? throw new InvalidOperationException("Connection string 'HangfireConnection' not found.");
builder.Services.AddDbContextFactory<SampleDbContext>(builder => builder.UseSqlite(connectionString));
builder.Services.AddHangfire((serviceProvider, configuration) =>
configuration.UseEFCoreStorage(
() => serviceProvider.GetRequiredService<IDbContextFactory<SampleDbContext>>().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),
}));
And then in the OnModelCreating
method of the DbContext class:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.OnHangfireModelCreating();
}
There is only built-in SQL-based internal queue provider supported. Additional providers support will be implemented in future.
Hangfire.EntityFrameworkCore licensed under the MIT License.