When enqueuing a job to Hangfire, it seems like the DI is not working as it should, since it seems to be trying to instantiate a class out of an interface.
In this example, trying to enqueue a job with Hangfire that involves using a class from DI directly within the job will result in a compilation error or runtime exception. Hangfire struggles with the abstract nature of the base class, as it can't create an instance directly.
The error output is the following:
What seems to be missing in my configuration?
Currently I have this setup:
{
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices(services =>
{
services
.AddOptions<DatabaseOptions>()
.BindConfiguration(DatabaseOptions.ConfigKey)
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddDbContextPool<AppDbContext>((provider, builder) =>
{
var dbOptions = provider
.GetRequiredService<IOptions<DatabaseOptions>>()
.Value;
builder
.UseNpgsql(dbOptions.Postgres.ConnectionString,
options => { options.CommandTimeout(dbOptions.Postgres.CommandTimeoutInSeconds); })
.UseSnakeCaseNamingConvention();
});
services.AddDbContext<EventStoreDbContext>((provider, builder) =>
{
var dbOptions = provider
.GetRequiredService<IOptions<DatabaseOptions>>()
.Value;
builder
.UseNpgsql(dbOptions.Postgres.ConnectionString,
options => { options.CommandTimeout(dbOptions.Postgres.CommandTimeoutInSeconds); })
.UseSnakeCaseNamingConvention();
});
services.AddHangfire((provider, configuration) =>
{
var dbOptions = provider.GetRequiredService<IOptions<DatabaseOptions>>().Value;
configuration.SetDataCompatibilityLevel(CompatibilityLevel.Version_170);
services.AddHangfireServer((provider, globalConfig) =>
{
});
services.AddTransient<IEventRepository, EventRepository>();
services.AddBlockchainInfrastructure();
});
return builder;
}
