问题描述
我需要在某个时间执行某些代码(在我的 ASP .NET Core 项目中).
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() {}
但是当它尝试使用我使用 DI 注入到 NotificationManager
构造函数中的 dbContext
时失败,说明它已被释放.
but fails when it tries to use my dbContext
which I injected using DI into the NotificationManager
constructor, stating that it was disposed.
这就是流";依赖项:
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}
}
startup.cs 中的 DI 设置:
DI setup in startup.cs:
services.AddDbContext<MyContext>();
services.AddScoped<INotificationManager, NotificationManager>();
推荐答案
选项
- 使用作业调度程序(如 Hangfire、Quartz.Net, Jobbr,...)
- 使用 后台服务 如果你的 .net 核心版本是 >= 2
- Use a job scheduler (Like Hangfire, Quartz.Net, Jobbr, ...)
- Use a background service if your .net core version is >= 2
在这两种情况下,您都需要在作业类中注入 DatabaseContext,否则您将收到 ObjectDisposedException.
In both cases you'll need to inject the DatabaseContext in the job class otherwise you'll receive an ObjectDisposedException.
当您需要横向扩展至多台机器时,您需要一个带有状态存储的作业服务器,例如 SQL Server、MSMQ、RabbitMQ、Redis...
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,...
Hangfire 示例
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))
请参阅 IBackgroundJobClient
这篇关于在 C# 中延迟任务 - 数据库上下文已处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!