Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
781 views
in Technique[技术] by (71.8m points)

asp.net core - Delaying a task in C# - db context disposed

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>();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...