Search code examples
azureservicebusazure-servicebus-queues

Get message correlation id in ProcessErrorAsync


I have an Azure Service Bus message consumer class, using queues.

I want to add the correlation id of the message to the logs I write. This is easily done in the Service Bus Processor's ProcessMessagesAsync method as I get the message as a parameter. But, I could not find a way to get the correlation id in the ProcessErrorAsync method.

The consuming service is registered as a singleton service in an ASP.NET Core app. I thought about using a local variable to store the correlation id when the message processing starts in the ProcessMessagesAsync method, and use that value in the ProcessErrorAsync method as well.

First question, is there a way to get the correlation id in the ProcessErrorAsync method?

Second question, is the local variable option feasible, or will it have issues if more then 1 concurrent call is handled at the same time?


Solution

  • There is not a way to correlate a specific message with an invocation of the ProcessErrorAsync handler. The handler is invoked for a number of reasons, many of which are not associated with a message.

    If there are actions that you'd like to take on a message if processing fails, it is recommended that you include a try/catch block in your ProcessMessagesAsync handler code to detect the processing error and perform those actions.