c# - Hangfire 后台作业保持排队

标签 c# asp.net-mvc-5 hangfire postal

我有一个 MVC 应用程序,我正在尝试使用 Hangfire 和 Postal 发送电子邮件。电子邮件必须在注册后发送。 注册工作正常,但我运行的作业仍在排队,我没有收到任何电子邮件。 所以在我的 MVC Controller 中,我有以下代码:

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>

如果我运行 hangfire 仪表板,我会看到已排队的作业

enter image description here

但没有其他事情发生。 发送电子邮件时我错过了什么?

谢谢

更新 在 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);
}

最佳答案

我发现了问题:

  1. sql server 的版本不受支持。我使用的是 2005。支持的数据库是 2008R2 及更高版本:http://docs.hangfire.io/en/latest/configuration/using-sql-server.html

  2. NotifyRegistration 方法必须是静态的: https://discuss.hangfire.io/t/jobs-in-enqueue-state-most-never-run/2367/4

.

[AutomaticRetry(Attempts = 5)]
public static void 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);
}

关于c# - Hangfire 后台作业保持排队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39531052/

相关文章:

c# - 装箱和拆箱

c# - 如何在没有 Controller 的情况下创建有效的 AuthenticationManager?

c# - 错误消息 "A potentially dangerous Request.Form value"

c# - 为什么返回类型是 IdentityUser 而不是 ApplicationUser?

c# - 在 HangFire 中设置一个只有 "on demand"的作业

c# - 如何使用 Hangfire 获取失败的后台作业的异常详细信息

c# - 如何验证表的MAX值?

c# - 如何防止 Mono 垂直拉伸(stretch) WinForms 应用程序中的窗口(在 Linux 上)?

c# - WebAPI + OWIN + SignalR + Autofac

c# - 如何以编程方式关闭 Windows 10 应用程序上的打印 UI?