Hangfire 重复作业不工作 - 抛出异常:"Unable to find a constructor"
Hangfire Recurring jobs not working - throw exception: "Unable to find a constructor"
我有 .net 核心 2.1 网络应用程序和 Hangfire 库 (1.7.9) 在 postgres (12) 上工作。
我尝试添加循环作业,但每次 Hangfire 尝试启动它时,它都会抛出异常:
"Hangfire.Common.JobLoadException: Could not load the job. See inner exception for the details. ---> Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type Microsoft.Extensions.FileProviders.PhysicalFileProvider. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Providers[0].Source.FileProvider.Root', line 1, position 490."
我的启动文件(Hangfire 部分):
private void ConfigureHangfire(IServiceCollection services)
{
var settings = Configuration.GetSettings<HangfireSettings>();
GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UsePostgreSqlStorage(Configuration.GetConnectionString("service_user"), new PostgreSqlStorageOptions
{
SchemaName = "hangfire",
QueuePollInterval = TimeSpan.FromSeconds(15),
UseNativeDatabaseTransactions = true,
PrepareSchemaIfNecessary = true,
TransactionSynchronisationTimeout = TimeSpan.FromMinutes(15)
});
var options = new PostgreSqlStorageOptions
{
PrepareSchemaIfNecessary = settings.CreateDatabaseAutomatically
};
services.AddHangfire(config => config.UsePostgreSqlStorage(Configuration.GetConnectionString("service_user"), options));
DailyDatabaseUpdate.TurnOnDailyUpdateScheduleWorker(9, 51, Configuration);
}
测试后我意识到我的方法 DailyDatabaseUpdate.TurnOnDailyUpdateScheduleWorker(9, 51, Configuration); 包含参数 IConfiguration 配置 .当我删除它时,一切正常。
但是这个接口对于获取我的连接字符串至关重要。
如何解决?
一个好的解决方案是将连接字符串设置为 IOption 或类似的东西,例如将配置绑定到某个对象,然后构造函数将此对象注入到 class 中,其中包含充当重复作业的方法。
示例:
public StatusJobs(DbOptions dbOptions)
{
_dbOptions= dbOptions
}
[DisableConcurrentExecution(timeoutInSeconds: 20)]
[AutomaticRetry(Attempts = 0, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
public void DoSmth()
{
//dbOptions avalaible here
}
此外,最好注入一些 connectionfactory 而不是 connectiostring。
我有 .net 核心 2.1 网络应用程序和 Hangfire 库 (1.7.9) 在 postgres (12) 上工作。
我尝试添加循环作业,但每次 Hangfire 尝试启动它时,它都会抛出异常:
"Hangfire.Common.JobLoadException: Could not load the job. See inner exception for the details. ---> Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type Microsoft.Extensions.FileProviders.PhysicalFileProvider. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Providers[0].Source.FileProvider.Root', line 1, position 490."
我的启动文件(Hangfire 部分):
private void ConfigureHangfire(IServiceCollection services)
{
var settings = Configuration.GetSettings<HangfireSettings>();
GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UsePostgreSqlStorage(Configuration.GetConnectionString("service_user"), new PostgreSqlStorageOptions
{
SchemaName = "hangfire",
QueuePollInterval = TimeSpan.FromSeconds(15),
UseNativeDatabaseTransactions = true,
PrepareSchemaIfNecessary = true,
TransactionSynchronisationTimeout = TimeSpan.FromMinutes(15)
});
var options = new PostgreSqlStorageOptions
{
PrepareSchemaIfNecessary = settings.CreateDatabaseAutomatically
};
services.AddHangfire(config => config.UsePostgreSqlStorage(Configuration.GetConnectionString("service_user"), options));
DailyDatabaseUpdate.TurnOnDailyUpdateScheduleWorker(9, 51, Configuration);
}
测试后我意识到我的方法 DailyDatabaseUpdate.TurnOnDailyUpdateScheduleWorker(9, 51, Configuration); 包含参数 IConfiguration 配置 .当我删除它时,一切正常。 但是这个接口对于获取我的连接字符串至关重要。
如何解决?
一个好的解决方案是将连接字符串设置为 IOption 或类似的东西,例如将配置绑定到某个对象,然后构造函数将此对象注入到 class 中,其中包含充当重复作业的方法。
示例:
public StatusJobs(DbOptions dbOptions)
{
_dbOptions= dbOptions
}
[DisableConcurrentExecution(timeoutInSeconds: 20)]
[AutomaticRetry(Attempts = 0, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
public void DoSmth()
{
//dbOptions avalaible here
}
此外,最好注入一些 connectionfactory 而不是 connectiostring。