Azure Service Bus: should I close QueueClient on application shutdown?

Viewed 316

I have an ASP.NET REST service that receives messages from a queue using Microsoft.Azure.ServiceBus library.

It has the next service bus queue messages consumer that instantiates QueueClient and starts listening to the messages from the queue:

public class ServiceBusConsumer : IServiceBusConsumer
{
    private readonly IQueueClient client;

    public ServiceBusConsumer()
    {
        client = new QueueClient(ServiceBusConnectionString, ServiceBusQueue);
        client.RegisterMessageHandler(ProcessMessagesAsync, ProcessErrorAsync);
    }

    // ...
}

My question is, how to dispose the instance of QueueClient on application shutdown correctly? Should I make the ServiceBusConsumer disposable and add a method like the below one to it, so that the dependency injection container would call it and dispose the QueueClient? Or the DisposeAsync is not needed?

public async ValueTask DisposeAsync()
{
    await client.UnregisterSessionHandlerAsync(maxMessageHandlingTime);
    await client.CloseAsync();
}

My confusion is caused by the fact that the QueueClient itself implements neither IDisposible, nor IAsyncDisposable, which suggests that I am not expected to perform any additional steps on application shutdown. On the other hand, the QueueClient has the CloseAsync method that suggests that some actions may be required for the graceful shutdown.

The official documentation only says that I should not close the QueueClient for every message, however, it does not say anything about closing QueueClient on application shutdown.

0 Answers
Related