问题
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:
Options
- Use a job scheduler (Like Hangfire, Quartz.Net, Jobbr, ...)
- Use a background service if your .net core version is >= 2
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
来源:https://stackoverflow.com/questions/63722040/delaying-a-task-in-c-sharp-db-context-disposed