Rob Lyndon
Rob Lyndon

Reputation: 12651

Azure Service Bus: AcceptMessageSession times out

For my Service Bus implementation, I need to fragment messages that exceed a certain size, and then reassemble them when all the fragments have been received.

When using competing queue clients (e.g. multiple worker role instances), it is useful to use sessions to ensure that the same instance receives all messages with a matching session ID. According to all of the online tutorials I've come across, this is achieved as follows:

_namespaceManager = CreateNamespaceManager(sharedAccessKey, nameSpace);
_messagingFactory = MessagingFactory.Create(_namespaceManager.Address, _namespaceManager.Settings.TokenProvider);
var qd = new QueueDescription("SessionQueue") { RequiresSession = true };
_namespaceManager.CreateQueue(qd);
var sender = _messagingFactory.CreateQueueClient("SessionQueue");
var qc = _messagingFactory.CreateQueueClient("SessionQueue", ReceiveMode.ReceiveAndDelete);
var ms = qc.AcceptMessageSession();

However, the last line always hangs and eventually times out. I've tried using different connecting protocols, like creating the namespace manager using NamespaceManager.CreateFromConnectionString, but the result is exactly the same.

Is there any reason why AcceptMessageSession might hang like this?

Here is the implementation of CreateNamespaceManager:

public static NamespaceManager CreateNamespaceManager(string sharedAccesskey, string nameSpace)
{            
    var uri = ServiceBusEnvironment.CreateServiceUri("sb", nameSpace, string.Empty);
    var tP = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", sharedAccesskey);
    return new NamespaceManager(uri, tP);
}

Upvotes: 0

Views: 1206

Answers (2)

Dejan Grujić
Dejan Grujić

Reputation: 417

Maybe not 100% related to the original question, but if any poor soul ends up here searching for why AcceptMessageSession times out when session is empty - it could also happen if you have Prefetch turned on, i.e. set to non-zero value.

Upvotes: 1

Slava Asipenko
Slava Asipenko

Reputation: 390

Are there messages already in the queue when AcceptMessageSession is called? If not, my understanding this is expected behavior. AcceptMessageSession will wait until the first message, from which it will grab SessionId to initialize the session. If you know your session id(s) in advance, you can try calling overloaded versions of AcceptMessageSession where you can specify session id and timeouts. Those will probably not block, but I am not 100% sure.

Upvotes: 1

Related Questions