空中射击经常性作业不起作用-抛出异常:“无法找到构造函数”

问题描述 投票:0回答:1

我有在.NET Core 2.1 Web APP上使用Hangfire库(1.7.9)在postgres(12)上工作。

我尝试添加周期性工作,但是每次Hangfire尝试触发它时,都会引发异常:

“ Hangfire.Common.JobLoadException:无法加载作业。有关详细信息,请参见内部异常。---> Newtonsoft.Json.JsonSerializationException:无法找到用于类型Microsoft.Extensions.FileProviders.PhysicalFileProvider的构造函数类应该具有默认构造函数,一个带有参数的构造函数或带有JsonConstructor属性标记的构造函数。路径'Providers [0] .Source.FileProvider.Root',第1行,位置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配置。当我删除它时,一切正常。但是,这种相互关系对于获取我的连接字符串至关重要。

如何解决?

postgresql .net-core hangfire
1个回答
0
投票

一个好的解决方案是将连接字符串设置为IOption或类似的绑定对象设置到某个对象,然后将该对象构造器注入到一个类中,该类包含作为重复工作的方法。

示例:

public StatusJobs(DbOptions dbOptions)
{
    _dbOptions= dbOptions
}

[DisableConcurrentExecution(timeoutInSeconds: 20)]
[AutomaticRetry(Attempts = 0, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
public void DoSmth()
{
    //dbOptions avalaible here
}

此外,最好注入一些connectionfactor而不是connectiostring。

© www.soinside.com 2019 - 2024. All rights reserved.