c# - 将数据库上下文注入 Hangfire Recurring 作业的正确方法是什么?
问题描述
我正在使用 HangFire 定期在后台向用户发送电子邮件。
我正在从数据库中获取电子邮件地址,但我不确定我是否将数据库上下文“注入”到负责正确发送电子邮件的服务中
这工作正常,有没有更好的方法来做到这一点?
public void Configure(IApplicationBuilder app, IHostingEnvironment env, Context context)
{
(...)
app.UseHangfireDashboard();
app.UseHangfireServer(new BackgroundJobServerOptions
{
HeartbeatInterval = new System.TimeSpan(0, 0, 5),
ServerCheckInterval = new System.TimeSpan(0, 0, 5),
SchedulePollingInterval = new System.TimeSpan(0, 0, 5)
});
RecurringJob.AddOrUpdate(() => new MessageService(context).Send(), Cron.Daily);
(...)
app.UseMvc();
}
public class MessageService
{
private Context ctx;
public MessageService(Context c)
{
ctx = c;
}
public void Send()
{
var emails = ctx.Users.Select(x => x.Email).ToList();
foreach (var email in emails)
{
sendEmail(email, "sample body");
}
}
}
解决方案
我只是查看了类似的问题,并没有在一个地方找到信息,所以在这里发布我的解决方案。
假设您已Context
配置为服务,即
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
....
services.AddDbContext<Context>(options => { ... });
....
}
这使得IServiceProvider
能够解决Context
依赖关系。
接下来,我们需要更新MessageService
类,以免Context
永远持有它,而仅实例化它以执行任务。
public class MessageService
{
IServiceProvider _serviceProvider;
public MessageService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public void Send()
{
using (IServiceScope scope = _serviceProvider.CreateScope())
using (Context ctx = scope.ServiceProvider.GetRequiredService<Context>())
{
var emails = ctx.Users.Select(x => x.Email).ToList();
foreach (var email in emails)
{
sendEmail(email, "sample body");
}
}
}
}
最后我们让Hangfire为我们实例化MessageService
,它也会很好地IServiceProvider
为我们解决依赖关系:
RecurringJob.AddOrUpdate<MessageService>(x => x.Send(), Cron.Daily);
推荐阅读
- msbuild - 在 MSBuild 中忽略/禁用环境变量
- json - 如何用逗号 JsonFormat 长值
- kotlin - Kotlin 编译器使用 Spring 的 TypeDescriptor 报告“没有方法 'equals(Any?): Boolean available”
- assembly - 片选位代表什么以及它们如何影响整体内存地址?
- python - 使用 Matplotlib 绘制点显示不正确
- sql - 处理从 DMV 提取的单行查询中的注释的 SQL 格式化选项
- python - 对于较旧的日期(即 1776 年),星期几出现错误
- regex - 正则表达式匹配正斜杠后跟字符串
- c# - iText7 - 将 HTML 转换为 PDF 时未呈现 CSS3 文本溢出属性
- hibernate - 使用休眠条件在 4 列之间选择 MAX 日期