public async Task ReceiverThrowsAfterSessionLockLost(bool isSessionSpecified) { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true, lockDuration: TimeSpan.FromSeconds(5))) { await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString); ServiceBusSender sender = client.CreateSender(scope.QueueName); var sessionId1 = "sessionId1"; await sender.SendAsync(GetMessage(sessionId1)); // send another session message before the one we are interested in to make sure that when isSessionSpecified=true, it is being respected await sender.SendAsync(GetMessage("sessionId2")); ServiceBusSessionReceiver receiver = await client.CreateSessionReceiverAsync(scope.QueueName, sessionId : isSessionSpecified?sessionId1 : null); if (isSessionSpecified) { Assert.AreEqual(sessionId1, receiver.SessionId); } var message = await receiver.ReceiveAsync(); await Task.Delay((receiver.SessionLockedUntil - DateTime.UtcNow) + TimeSpan.FromSeconds(5)); Assert.That(async() => await receiver.ReceiveAsync(), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); Assert.That(async() => await receiver.SetSessionStateAsync(null), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); Assert.That(async() => await receiver.GetSessionStateAsync(), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); Assert.That(async() => await receiver.CompleteAsync(message), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); Assert.That(async() => await receiver.CompleteAsync(message), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); Assert.That(async() => await receiver.DeadLetterAsync(message), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); Assert.That(async() => await receiver.DeferAsync(message), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); Assert.That(async() => await receiver.AbandonAsync(message), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); } }