Reputation: 2359
I wan't to add some configuration to hangfire. It's easy going the documented way but there is one option that depends on User setting so I wan't to do it like this:
IGlobalConfiguration hangfireConfiguration = GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings();
if (Configuration.GetValue<bool>("HangfireUseMemoryStorage"))
{
hangfireConfiguration.UseMemoryStorage();
}
else
{
hangfireConfiguration.UseStorage(new MySqlStorage(
Configuration.GetConnectionString("DefaultConnection"),
new MySqlStorageOptions
{
TablesPrefix = "Hangfire"
})
);
};
But how to add a service with this configuration? Trying
services.AddHangfire(hangfireConfiguration);
leads to
cannot convert from 'Hangfire.IGlobalConfiguration' to 'System.Action<Hangfire.IGlobalConfiguration>'
So how can I add my configuration?
Upvotes: 1
Views: 5467
Reputation: 11364
If you want to define the hangfire configuration, you will need to add it like this,
// Add Hangfire services.
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection"), new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true
}));
// Add the processing server as IHostedService
services.AddHangfireServer();
You can add the if/else on which storage to use based on Configuration.GetValue<bool>("HangfireUseMemoryStorage")
Upvotes: 2