0

我有一个 ASP.NET 应用程序,只要用户在网站上注册,它就会发送电子邮件。我正在使用 hangfire 来管理工作和邮政以发送电子邮件。

这一切都很好,但事情是这样的:

我希望超级用户在删除作业之前更改 APP 可以发送电子邮件的次数。

这是我的代码

    public static void WelcomeUser(DBContexts.Notifications not)
    {
        try{
            var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
            var engines = new ViewEngineCollection();
            engines.Add(new FileSystemRazorViewEngine(viewsPath));

            Postal.EmailService service = new Postal.EmailService(engines);

            WelcomeUserMail welcomeUserMail = new WelcomeUserMail();
            welcomeUserMail.To = not.ReceiverEmail;
            welcomeUserMail.UserEmail = not.ReceiverEmail;
            welcomeUserMail.From = BaseNotification.GetEmailFrom();

            service.Send(welcomeUserMail);
        }
        catch(Exception e)
        {
            DBContexts.DBModel dbModel = new DBModel();
            DBContexts.Notifications notificacionBD =  dbModel.Notifications.Find(not.NotificationID);

            notificacionBD.Status = false;
            notificacionBD.Timestamp = DateTime.Now;
            notificacionBD.Error = e.Message;

            int numberOfRetriesAllowed = ParameterHelper.getNumberOfRetriesAllowed();

            if (notificacionBD.Retries > numberOfRetriesAllowed)
            {
                //In this case Hangfire won't put this job in the failed section but rather in the processed section.
                dbModel.SaveChanges();
            }
            else
            {
                notificacionBD.Retries++;
                dbModel.SaveChanges();

                throw new Exception(e.Message);
            }
        }
    }
4

1 回答 1

0

为什么不直接添加属性来自动处理呢?

[AutomaticRetry(Attempts = 10, LogEvents = true, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
public void MyTask(){
    //doing stuff
}

或者您可以创建自己的属性来模仿 AutommaticRetryAttribute 类,但您可以按照自己的方式处理它?

https://github.com/HangfireIO/Hangfire/blob/a5761072f18ff4caa80910cda4652970cf52e693/src/Hangfire.Core/AutomaticRetryAttribute.cs

于 2016-04-14T13:04:04.993 回答