private void HandleMessageResult(SubscriberInfo subscriberInfo, ServiceBusMessageStates state, IBrokeredMessage brokeredMessage, string messageBody) { _logService.Debug("Handling message id '{0}', topic/subscription '{1}', state '{2}', body '{3}' ", brokeredMessage.MessageId, subscriberInfo.SubscriptionPath, state, messageBody); switch (state) { case ServiceBusMessageStates.Complete: brokeredMessage.Complete(); break; case ServiceBusMessageStates.Abandon: brokeredMessage.Abandon(); break; case ServiceBusMessageStates.DeadLetter: brokeredMessage.DeadLetter(); break; case ServiceBusMessageStates.Requeue: var scheduledEnqueueTimeUtc = brokeredMessage.ScheduledEnqueueTimeUtc == DateTime.MinValue ? DateTime.UtcNow.AddSeconds(30) : GetDefaultRequeueDateTimeUtc(); var newBrokeredMessage = new BrokeredMessage(messageBody) { ScheduledEnqueueTimeUtc = scheduledEnqueueTimeUtc }; subscriberInfo.TopicClient.Send(newBrokeredMessage); brokeredMessage.Complete(); break; default: var deadLetterReason = string.Format( "Invalid message state '{0}' for message id '{1}', topic/subscription '{2}': message will be dead-lettered", state, brokeredMessage.MessageId, subscriberInfo.SubscriptionPath); _logService.Error(deadLetterReason); brokeredMessage.DeadLetter(); break; } _logService.Debug("Handled message id '{0}', topic/subscription '{1}', state '{2}', body '{3}' ", brokeredMessage.MessageId, subscriberInfo.SubscriptionPath, state, messageBody); }
static bool SafeAbandon(IBrokeredMessage msg) { try { // Abandons a brokered message. This will cause the Service Bus to unlock the message and make it available to be received again, // either by the same consumer or by another competing consumer. msg.Abandon(); // Return a result indicating that the message has been abandoned successfully. return(true); } catch (MessageLockLostException) { // It's too late to compensate the loss of a message lock. We should just ignore it so that it does not break the receive loop. // We should be prepared to receive the same message again. } catch (MessagingException) { // There is nothing we can do as the connection may have been lost, or the underlying topic/subscription may have been removed. // If Abandon() fails with this exception, the only recourse is to receive another message (possibly the same one). } return(false); }
static bool SafeAbandon(IBrokeredMessage msg) { try { // Abandons a brokered message. This will cause the Service Bus to unlock the message and make it available to be received again, // either by the same consumer or by another competing consumer. msg.Abandon(); // Return a result indicating that the message has been abandoned successfully. return true; } catch (MessageLockLostException) { // It's too late to compensate the loss of a message lock. We should just ignore it so that it does not break the receive loop. // We should be prepared to receive the same message again. } catch (MessagingException) { // There is nothing we can do as the connection may have been lost, or the underlying topic/subscription may have been removed. // If Abandon() fails with this exception, the only recourse is to receive another message (possibly the same one). } return false; }