Properly disposing of Azure Service Bus Client while a message is in the middle of being handled

Viewed 6459

When using async/await I'm concerned with disposing of the client while it's in the middle of handling a message. Consider the following:

  1. Initialize a queue client queueClient and store a reference to it in the global scope of the class

  2. Queue client handles a message and calls some application code to handle it, which might end up doing some async database work or call a remote api.

  3. Consider the application is a windows service with a CloseAsync method that is signaled when the service should be shutdown. Within this method I call queueClient.CloseAsync()

  4. The work being done in step 2 finishes and goes to call message.Complete(). At this point I'm assuming the queueClient has been closed and the message would be treated as a failure.

What is the best practice for ensuring that the queue client doesn't handle anymore messages and waits to close until any currently processing messages are finished?

1 Answers
Related