![](/img/trans.png)
[英]How to add a custom HttpContextAccessor to service ASP.NET Core
[英]How to add Hangfire as service with custom configuration on asp.net core
我不想向 hangfire 添加一些配置。 按照記錄的方式很容易,但有一個選項取決於用戶設置,所以我不想這樣做:
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"
})
);
};
但是如何使用這種配置添加服務呢? 試
services.AddHangfire(hangfireConfiguration);
造成
cannot convert from 'Hangfire.IGlobalConfiguration' to 'System.Action<Hangfire.IGlobalConfiguration>'
那么我該如何添加我的配置呢?
如果你想定義 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();
您可以根據Configuration.GetValue<bool>("HangfireUseMemoryStorage")
添加要在哪個存儲上使用的 if/else
我不想為hangfire 添加一些配置。 采用記錄方式很容易,但是有一個選項取決於用戶設置,所以我不想這樣做:
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"
})
);
};
但是如何使用這種配置添加服務呢? 試
services.AddHangfire(hangfireConfiguration);
導致
cannot convert from 'Hangfire.IGlobalConfiguration' to 'System.Action<Hangfire.IGlobalConfiguration>'
那么如何添加我的配置呢?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.