I am using Azure.Messaging.ServiceBus library.
I create ServiceBusClient and ServiceBusProcessor to start receiving queue messages like this:
var serviceBusClient = new ServiceBusClient(connectionString);
ServiceBusProcessor processor = serviceBusClient.CreateProcessor(queue);
processor.ProcessMessageAsync += HandleEvent;
processor.StartProcessingAsync();
On application shutdown I dispose ServiceBusClient and ServiceBusProcessor in this way:
processor.ProcessMessageAsync -= HandleEvent; // Is this needed?
await processor.DisposeAsync();
await serviceBusClient.DisposeAsync();
Do I need to remove the HandleEvent explicitly, or processor.DisposeAsync() should be responsible for that?
I cannot find that processor.DisposeAsync() is removing any handlers in its code, however, probably it is because removing the handlers is not needed at all? Official documentation seems also lacking any good examples of how to dispose instances of ServiceBusProcessor properly.
Can it be that await processor.DisposeAsync() is not needed at all, since serviceBusClient.DisposeAsync() should do all the job?