Darryl
Darryl

Reputation: 1541

Cancel an Azure Service Bus QueueClient long running Receive?

I am writing a test program to process messages in an Azure Service Bus Queue. I want to be able to provide the the ability to start and stop the dequeueing functionality.

If I am wondering if there is a way to cancel a call to QueueClient.Receive(TimeSpan)? There is a QueueClient.Abort() method, and also a QueueClient.Close() method, but the documentation on these is sparse. And, there does not seem to be a corresponding method to "Open" after aborting or closing.

If there is no way to cancel a long running receive, then I am stuck using a short (or no) TimeSpan in order to get a chance for the user to stop dequeueing. And by doing that I'm pretty much doing polling myself, which defeats the advantage of the long running receive functionality.

I'm new to using the Service Bus Queue, so maybe I'm on the wrong track with my approach here, and not understanding the spirit of how messages in the queue are supposed to be processed?

Upvotes: 2

Views: 2871

Answers (3)

Thomas
Thomas

Reputation: 2458

Close the message factory that created the queue client and the receiver will abort. Closing a messaging factory deletes the connection to the Service Bus service. Note that other MessagingEntities that have been created from the factory are also closed so use a dedicated factory for the receive loop.

var myFactory = MessagingFactory.FromConnectionString(...);
myFactory.Close();

The Abort method on the QueueClient sometimes doesn't abort the actual receive operation within a reasonable time period in my experience.

To start receiving again you will need to create a new QueueClient from a new MessagingFactory.

Upvotes: 3

Clemens Vasters
Clemens Vasters

Reputation: 2686

The by-design solution is indeed to close the client.

Upvotes: 1

Timothy Khouri
Timothy Khouri

Reputation: 31845

Likely you will have this call on it's own background thread, to which you can simply kill the thread itself. Side point, you should close the QueueClient object first.

When you are ready to receive again, create a new instance, and begin your background thread.

Upvotes: 1

Related Questions