3
votes

I have a situation where I need certain code to execute at a certain time (in my ASP .NET Core project).

I know that delaying a task is not a great solution, but this is what I have and I'd like to know how to make it work:

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
}

When I try to run the code, it enters the method that is supposed to be executed, at the right time:

public async Task CreateReminder() {}

but fails when it tries to use my dbContext which I injected using DI into the NotificationManager constructor, stating that it was disposed.

This is the "flow" of the dependencies:

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 setup in startup.cs:

services.AddDbContext<MyContext>(); 
services.AddScoped<INotificationManager, NotificationManager>();
1
Just curious: why not await Task.Delay(time); await notificationManager.CreateReminder(); ?Fildor
@Fildor In startup.cs: services.AddDbContext<MyContext>(); services.AddScoped<INotificationManager, NotificationManager>();Eutherpy
I added that into your question. Hope you don't mind.Fildor
@Arman I ended up using the Hangfire library, it's really simple to use and does everything I need!Eutherpy

1 Answers

4
votes

Options

In both cases you'll need to inject the DatabaseContext in the job class otherwise you'll receive an ObjectDisposedException.

When you need to scale-out to multiple machines you'll need a job server with a state store like SQL Server, MSMQ, RabbitMQ, Redis,...

Sample with 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))

See the docs for IBackgroundJobClient