[英]How to add Hangfire as service with custom configuration on asp.net core
I wan't to add some configuration to hangfire.我不想向 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?那么我该如何添加我的配置呢?
If you want to define the hangfire configuration, you will need to add it like this,如果你想定义 hangfire 配置,你需要像这样添加它,
// 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")
您可以根据
Configuration.GetValue<bool>("HangfireUseMemoryStorage")
添加要在哪个存储上使用的 if/else
I wan't to add some configuration to hangfire.我不想为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?那么如何添加我的配置呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.