public async Task SendEmailAsync(string email, string subject, string body) { var message = AbstractTypeFactory <NotificationMessage> .TryCreateInstance(nameof(EmailNotificationMessage)); if (message is EmailNotificationMessage notificationMessage) { notificationMessage.From = _emailSendingOptions.DefaultSender; notificationMessage.To = email; notificationMessage.Subject = subject; notificationMessage.Body = body; } await _notificationMessageSenderProviderFactory.GetSenderForNotificationType(nameof(EmailNotification)).SendNotificationAsync(message); }
public async Task <NotificationSendResult> SendNotificationAsync(Notification notification, string language) { if (notification == null) { throw new ArgumentNullException(nameof(notification)); } var result = new NotificationSendResult(); var message = AbstractTypeFactory <NotificationMessage> .TryCreateInstance($"{notification.Kind}Message"); message.LanguageCode = language; message.MaxSendAttemptCount = _maxRetryAttempts + 1; notification.ToMessage(message, _notificationTemplateRender); await _notificationMessageService.SaveNotificationMessagesAsync(new[] { message }); var policy = Policy.Handle <SentNotificationException>().WaitAndRetryAsync(_maxRetryAttempts, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)) , (exception, timeSpan, retryCount, context) => { _logger.LogError(exception, $"Retry {retryCount} of {context.PolicyKey}, due to: {exception}."); message.LastSendError = exception?.Message; }); var policyResult = await policy.ExecuteAndCaptureAsync(() => { message.LastSendAttemptDate = DateTime.Now; message.SendAttemptCount++; return(_notificationMessageAccessor.GetSenderForNotificationType(notification.Kind).SendNotificationAsync(message)); }); if (policyResult.Outcome == OutcomeType.Successful) { result.IsSuccess = true; message.SendDate = DateTime.Now; } else { result.ErrorMessage = policyResult.FinalException?.Message; } await _notificationMessageService.SaveNotificationMessagesAsync(new[] { message }); return(result); }