/// <summary> /// Completes a message by doing the actual READ from the queue. /// </summary> /// <param name="message">The message we want to complete.</param> /// <returns>Task.</returns> internal async Task Complete(T message) { try { if (Messages.TryGetValue(message, out var msg)) { Release(message); if (msg.SystemProperties.LockedUntilUtc < DateTime.Now) { Receiver.RenewLockAsync(msg.SystemProperties.LockToken).GetAwaiter().GetResult(); } await Receiver.CompleteAsync(msg.SystemProperties.LockToken).ConfigureAwait(false); } } catch (Exception ex) when(ex is MessageLockLostException || ex is MessageNotFoundException) { Logger?.LogWarning(ex, "Error during message Complete, lock was lost [THIS CAN BE IGNORED - already in use or already processed]"); } catch (Exception e) { Logger?.LogError(e, "Error during message Complete"); MessageIn?.OnError(e); } }
private async Task <IEnumerable <string> > ReceiveMessages(string id) { var lockTokens = new List <string>(); Message message; do { message = await Receiver.ReceiveAsync(TimeSpan.FromSeconds(5)); if (message == null) { Console.WriteLine($"Message with id {id} was not found"); break; } if (string.IsNullOrEmpty(id) || message.MessageId == id) { await Receiver.CompleteAsync(message.SystemProperties.LockToken); Console.WriteLine($"Message with id {message.MessageId} was removed"); } lockTokens.Add(message.SystemProperties.LockToken); } while (message != null); return(lockTokens); }
[ExcludeFromCodeCoverage] // Skipped - too many edge cases that cant be tested. internal async Task CompleteAll(CancellationTokenSource tokenSource = null) { do { // Check for operation cancellation. if (tokenSource != null && tokenSource.IsCancellationRequested) { break; } try { Receiver.PrefetchCount = 100; // Get a single message from the receiver entity. var messages = await Receiver.ReceiveAsync(1, TimeSpan.FromSeconds(5)).ConfigureAwait(false); if (messages == null) { // If we have null returned, double check we aren't getting null when there are // actually items on the entity - BUG FIX FOR SB. if (BrokenReceiverCheck()) { messages = await Receiver.ReceiveAsync(1, TimeSpan.FromSeconds(10)).ConfigureAwait(false); } // If messages are still null - do the count and potentially finish off. if (messages == null) { var messageCount = await GetReceiverMessageCount(); if (messageCount.ActiveEntityCount == 0) { break; } } } if (messages != null) { foreach (var msg in messages) { await Receiver.CompleteAsync(msg.SystemProperties.LockToken); } } } catch (Exception e) { Logger?.LogError(e, "Error during read of service bus message"); } } while (true); }
public async Task Execute() { Message message; int count = 0; do { message = await Receiver.ReceiveAsync(TimeSpan.FromSeconds(5)); if (message != null) { await Sender.SendAsync(Copy(message)); await Receiver.CompleteAsync(message.SystemProperties.LockToken); count++; } } while (message != null); Console.WriteLine($"Resent {count} messages to queue/subscription"); }