【问题标题】:.net core hangfire run a specific task.net core hangfire 运行特定任务
【发布时间】:2021-09-26 08:08:09
【问题描述】:

我正在使用挂火。我需要一个运行一次的任务,如下所示。我该怎么做?

 BackgroundJob.Schedule(() => new EmailService().SendEmail("new email.."), "10 05 * * *");

错误

 Severity   Code    Description Project File    Line    Suppression State

错误 CS1662 无法将 lambda 表达式转换为预期的委托类型,因为块中的某些返回类型不能隐式转换为委托返回类型 My.API

【问题讨论】:

  • SendEmail 方法有任何返回类型吗?

标签: c# .net visual-studio asp.net-core hangfire


【解决方案1】:

很遗憾,我们看不到您的 SendEmail 方法的代码,但它应该是这样的并且应该可以工作。

public Task SendEmail()
{
    // Send email

    return Task.CompletedTask;
}

要安排:

public void TestMethod()
{
    BackgroundJob.Schedule(() => new EmailService().SendEmail(), TimeSpan.FromMinutes(30));
}

【讨论】:

  • 你好,如你所说。但我如何将其设置为特定日期。即如何将其设置为小时/日/月/年。
  • BackgroundJob.Schedule(() => new EmailService().SendEmail("new message"), "10 05 * * *");
  • 您可以通过执行以下操作来做到这一点:BackgroundJob.Schedule(() => new EmailService().SendEmail(), new DateTimeOffset(new DateTime(2021, 10, 22)));
  • 非常感谢你,但是我怎样才能添加时钟
  • 日期时间(2021, 10, 22, 15:00)))
猜你喜欢
  • 2020-01-22
  • 1970-01-01
  • 2020-11-20
  • 2017-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
相关资源
最近更新 更多