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:
Initialize a queue client
queueClientand store a reference to it in the global scope of the classQueue 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.
Consider the application is a windows service with a
CloseAsyncmethod that is signaled when the service should be shutdown. Within this method I callqueueClient.CloseAsync()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?