Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Hangfire:调用具有延迟的方法不会';行不通_C#_.net_Postgresql_Asp.net Core_Hangfire - Fatal编程技术网

C# Hangfire:调用具有延迟的方法不会';行不通

C# Hangfire:调用具有延迟的方法不会';行不通,c#,.net,postgresql,asp.net-core,hangfire,C#,.net,Postgresql,Asp.net Core,Hangfire,我有一个ASP.NET核心应用程序,希望使用Hangfire执行一些后台任务。 我设置了官方文档中描述的Hangfire,我可以称之为普通的后台任务。 但是,我在延迟调用方法时遇到问题 public void Configure(/*other params*/ IBackgroundJobClient backgroundJobsClient) { //Other code backgroundJobsClient.Enqueue(() => Console.Writ

我有一个ASP.NET核心应用程序,希望使用Hangfire执行一些后台任务。 我设置了官方文档中描述的Hangfire,我可以称之为普通的后台任务。 但是,我在延迟调用方法时遇到问题

public void Configure(/*other params*/ IBackgroundJobClient backgroundJobsClient)
{
   //Other code
   
   backgroundJobsClient.Enqueue(() => Console.WriteLine("Method"));
   backgroundJobsClient.Schedule(() => Console.WriteLine("Delayed Method"), TimeSpan.FromSeconds(2));
}

控制台中只有“方法”输出

等待一分钟后,我的控制台出现异常:

Hangfire.Processing.BackgroundExecution[0] 由于异常,Execution DelayedJobScheduler现在处于失败状态,执行重试时间不超过00:00:04 Hangfire.PostgreSql.PostgreSqlDistributedLockException:无法对资源“Hangfire:locks:schedulepoller”进行锁定:锁定超时。 在Hangfire.PostgreSql.postgresqldributedlock.postgresqldributedlock_Init_事务(字符串资源、时间跨度超时、IDbConnection连接、PostgreSqlStorageOptions) 在Hangfire.PostgreSql.postgresqldributedlock..ctor(字符串资源、时间跨度超时、IDbConnection连接、PostgreSqlStorageOptions) 位于Hangfire.PostgreSql.PostgreSqlConnection.AcquiredDistributedLock(字符串资源,TimeSpan超时) 在Hangfire.Server.DelayedJobScheduler.UseConnectionDistributedLock[T](作业存储,Func
2操作)在Hangfire.Server.DelayedJobScheduler.EnqueueNextScheduledJobs(BackgroundProcessContext上下文)在Hangfire.Server.DelayedJobScheduler.Execute(BackgroundProcessContext上下文)在Hangfire.Processing.BackgroundExecution.Run(操作
2回调,对象状态)处的Hangfire.Server.BackgroundProcessDispatcherBuilder.ExecuteProcess(Guid executionId,对象状态) !!! 无法对资源“HangFire:locks:schedulepoller”设置锁:锁超时

因此,我认为延迟的方法初始化存在一些问题。
有人能帮我吗?可能我没有为ASP.NET core正确设置Hangfire。

我终于找到了解决方案。 日期以UTC格式存储在Hangfire数据库中。 我不知道这一点,官方也没有对此发表任何评论。 正确的代码应该如下所示:

DateTime.UtcNow.AddSeconds(2);

似乎您正在postgress数据库上获得锁定超时:`无法对资源设置锁定'HangFire:locks:schedulepoller':锁定超时'。您是否正在使用
禁用ConcurrentExecutionAttribute
?感谢您的帮助。这个属性帮助我摆脱了异常(我用自己的方法包装了Console.WriteLine并将该属性应用于它)。但是,我仍然看不到延迟任务的输出。请参阅在Asp.net核心应用程序中使用Hangfire,我已经按照步骤配置了设置并注册了服务,然后延迟方法在我这边运行良好。最后,如果仍然无法工作,最好在ConfigureServices方法中发布相关代码,以便我们了解如何注册Hangfire服务。感谢您的评论。当我开始使用Hangfire时,我阅读了本教程。我找到了问题的原因。请看我的答案。