/// <summary> /// Starts listening for session messages on the configured Service Bus Queue. /// </summary> protected override void ListenForSessionMessages() { ThreadStart ts = async() => { WriteLog($"Service Bus Communication Listnener now listening for session messages."); while (!StopProcessingMessageToken.IsCancellationRequested) { MessageSession session = null; try { session = ServiceBusClient.AcceptMessageSession(ServerTimeout); do { var messages = session.ReceiveBatch(MessageBatchSize, ServerTimeout).ToArray(); if (messages.Length == 0) { break; } var tokens = BatchReceiver.AutoComplete ? messages.Select(m => m.LockToken).ToArray() : null; await ProcessMessagesAsync(messages, session); if (BatchReceiver.AutoComplete) { await session.CompleteBatchAsync(tokens); } }while (!StopProcessingMessageToken.IsCancellationRequested); } catch (TimeoutException) { } finally { session?.Close(); } } }; StartBackgroundThread(ts); }