在 C# 中延迟任务 - 释放数据库上下文

Delaying a task in C# - db context disposed

我有一种情况需要在特定时间执行某些代码(在我的 ASP .NET Core 项目中)。

我知道延迟任务不是一个很好的解决方案,但这是我所拥有的,我想知道如何让它发挥作用:

async Task MyMethod()
{
  // do something
  // Create a new thread that waits for the appropriate time
      TimeSpan time = dbAppointment.ScheduledOn - TimeSpan.FromMinutes(5.0) - DateTime.UtcNow;
      _ = Task.Delay(time).ContinueWith(async x => await 
          notificationManager.CreateReminder());
  // continue doing something
}

当我尝试 运行 代码时,它在正确的时间进入了应该执行的方法:

public async Task CreateReminder() {}

但是当它尝试使用我的 dbContext 时失败,我使用 DI 将其注入到 NotificationManager 构造函数中,说明它已被处置。

这是依赖项的“流程”:

public class MyClass
{
  private readonly MyContext dbContext;
  private readonly INotificationManager notificationManager;
  public MyClass(MyContext context, INotificationManager nm) 
  { 
    dbContext = context;
    notificationManager = nm;
  }

  public async Task MyMethod() // the method shown in the snippet above
  { 
    // method does something using the dbContext

    _ = Task.Delay(time).ContinueWith(async x => await 
          notificationManager.CreateReminder());
  }
}

public class NotificationManager: INotificationManager
{
  private readonly MyContext dbContext;
  public NotificationManager(MyContext context) { dbContext = context;}
  public async Task CreateReminder() { // this method uses the dbContext}
}

DI 设置 startup.cs:

services.AddDbContext<MyContext>(); 
services.AddScoped<INotificationManager, NotificationManager>();

选项

在这两种情况下,您都需要在作业中注入 DatabaseContext class,否则您将收到 ObjectDisposedException。

当您需要 scale-out 到多台机器时,您需要一个带有状态存储的作业服务器,例如 SQL 服务器、MSMQ、RabbitMQ、Redis...

Hangfire 示例

public class MyDelayJob
{
   private readonly MyContext dbContext;
   private readonly INotificationManager notificationManager;
   public MyDelayJob(MyContext context, INotificationManager nm)
   {
       dbContext= context;
       notificationManager = nm;
   }

   public async Task Run(/*parameters*/)
   {
      await notificationManager.CreateReminder()
   }
}

/*Shedule code in MyMethod
    IBackgroundJobClient can be injected
    you need to register MyDelayJob with your IOC container.
 */

backgroundJobClient.Schedule<MyDelayJob>(j => j.Run(), TimeSpan.FromSeconds(60))

请参阅 IBackgroundJobClient

的文档