【发布时间】:2018-11-28 08:42:03
【问题描述】:
我正在使用 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");
}
}
}
【问题讨论】:
标签: c# asp.net-core .net-core hangfire