async Task <long> PurgeSessionEntity() { long totalMessagesPurged = 0; var consecutiveSessionTimeOuts = 0; ServiceBusSessionReceiver sessionReceiver = null; long messagesToPurgeCount = await GetMessageCount(deadLetterQueueData : false) .ConfigureAwait(false); var client = new ServiceBusClient( serviceBusHelper.ConnectionString, new ServiceBusClientOptions { TransportType = serviceBusHelper.TransportType }); try { const int enoughZeroReceives = 3; while (consecutiveSessionTimeOuts < enoughZeroReceives && totalMessagesPurged < messagesToPurgeCount) { sessionReceiver = await CreateServiceBusSessionReceiver( client, purgeDeadLetterQueueInstead : false) .ConfigureAwait(false); var consecutiveZeroBatchReceives = 0; while (consecutiveZeroBatchReceives < enoughZeroReceives && totalMessagesPurged < messagesToPurgeCount) { var messages = await sessionReceiver.ReceiveMessagesAsync( maxMessages : 1000, maxWaitTime : TimeSpan.FromMilliseconds(1000)) .ConfigureAwait(false); if (messages != null && messages.Any()) { Interlocked.Add(ref totalMessagesPurged, messages.Count()); consecutiveZeroBatchReceives = 0; } else { ++consecutiveZeroBatchReceives; } } await sessionReceiver.CloseAsync().ConfigureAwait(false); } } catch (TimeoutException) { ++consecutiveSessionTimeOuts; } finally { await client.DisposeAsync().ConfigureAwait(false); } return(totalMessagesPurged); }
public override async Task CloseAsync(CancellationToken cancellationToken = default) { await _receiver.CloseAsync(cancellationToken); }