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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…