我有一个MVC应用程序,我试图用Hangfire和Postal发送一封电子邮件。电子邮件必须在注册后发送。 注册工作正常,但我运行的工作仍然排队,我没有收到任何电子邮件。 所以在我的MVC控制器我有以下代码:Hangfire后台作业仍然排队
public async Task<ActionResult> Register(RegisterViewModel model)
{
//register correctly the user
//I send the email
BackgroundJob.Enqueue(() =>
NotifyRegistration(user.Id, user.UserName, user.Email)
);
...
}
[AutomaticRetry(Attempts = 5)]
public async Task NotifyRegistration(string userId, string username, string email)
{
//I calculate callbackUrl
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
var engines = new ViewEngineCollection();
engines.Add(new FileSystemRazorViewEngine(viewsPath));
var emailService = new EmailService(engines);
var emailToSend = new NewRegisteredUserEmail
{
To = email, UserName = username, CallbackUrl = callbackUrl
};
emailService.Send(emailToSend);
}
我不能调试NotifyRegistration方法。我不知道为什么。我使用Postal,所以EmailService不是我的实现。在这里,我如何配置SMTP服务:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.live.com" port="25" enableSsl="true" userName="***" password="***"></network>
</smtp>
</mailSettings>
</system.net>
如果我运行迟发型仪表盘我看到工作enqued
,但没有别的事。 我错过了什么发送电子邮件?
谢谢
UPDATE 在startup.cs我写了这个:
var options = new SqlServerStorageOptions
{
QueuePollInterval = TimeSpan.FromSeconds(1)
};
GlobalConfiguration.Configuration
.UseSqlServerStorage("DbConnectionString", options)
.UseFilter(new LogEmailFailureAttribute());
app.UseHangfireDashboard();
app.UseHangfireServer();
更新2 我改变了我这样NotifyRegistration:
[AutomaticRetry(Attempts = 5)]
public async Task NotifyRegistration(string userId, string username, string email, EmailService emailService)
{
//I calculate callbackUrl
var emailToSend = new NewRegisteredUserEmail
{
To = email, UserName = username, CallbackUrl = callbackUrl
};
emailService.Send(emailToSend);
}
'我无法调试NotifyRegistration方法。我不知道为什么'你可能正在运行一个较旧的文件状态? – Hristo
如果将呼叫移出挂火作业,是否会发送电子邮件? –
@克里斯,我试图做aclean和重建......没什么。我想我不能调试它,因为是后台作业...或类似 – Ciccio