2

Context: MSDN documentation on Service Bus queues and message sessions says "When the MessageSession object is accepted and while it is held by a client, that client holds an exclusive lock on all messages with that session's SessionId that exist in the queue or subscription, and also on all messages with that SessionId that still arrive while the session is held.

The lock is released when Close or CloseAsync are called, or when the lock expires in cases in which the application is unable to perform the close operation"

My Interpretation: I'm interpreting the above to mean that a MessageSession has a lock on a queue session, which is why other clients can't call AcceptMessageSessionAsync with the same SessionID - they'd get a SessionCannotBeLockedException.

Question: Assuming my interpretation is accurate, what controls when the MessageSession lock expires in cases where an application can't perform the Close operation? I know LockDuration on the queue doesn't accomplish this because I tried it.

Sample Code:

var sessionClient = new SessionClient(ServiceBusConnectionString, QueueName, ReceiveMode.PeekLock);
var session = await sessionClient.AcceptMessageSessionAsync("0");
// ReceiveAsync seems to wait for 60 seconds, 
// even if the LockDuration on the queue is 30 seconds. 
var message = await session.ReceiveAsync();
// Once ReceiveAsync returns, I'd expect to receive a SessionLockLost
// exception at some point since the lock should expire, according to MSDN
Console.ReadKey();
Craig
  • 1,890
  • 1
  • 26
  • 44

1 Answers1

2

Your interpretation is correct. What controls session lock is the connection to the broker. Once connection is lost, the session can be opened by another client. LockDuration is for individual messages, not session. So as long as the client is connected to the broker with an open session, that session will remain locked. Once connection is lost, the session will be opened by another requesting client. But only one client at a time to ensure ordered messages processing.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80