Azure function throws "Message processing error(Action=Complete)" after calling deadletterasync with autocomplete not setting as False. But when I set "Autocomplete=false" even after processing successful messages it keeps retrying. What is the best way to receive message ,and deadletter when exception occurs?
public async Task Test(
[ServiceBusTrigger(
topicName: "testTopic",
subscriptionName: "testSubsription",
Connection = "testConnectionString",Autocomplete = false)]
Message message,
MessageReceiver messageReceiver,
[ServiceBus("SendTopic", EntityType.Topic, Connection = "SendConnection")]
IAsyncCollector<Message> output,
CancellationToken cancellationToken)
{
try
{
var result = JsonConvert.DeserializeObject<TestObject>(Encoding.UTF8.GetString(message.Body));
foreach (var data in result.Data)
{
var convertedData= JsonConvert.SerializeObject(data);
var byteArray = Encoding.UTF8.GetBytes(convertedData);
Message outputMessages = new(byteArray);
await output.AddAsync(outputMessages, cancellationToken);
await messageReceiver.CompleteAsync(lockToken);
}
}
catch (Exception ex)
{
await messageReceiver.DeadLetterAsync(lockToken);
}
}
2
Answers
deadletterqueue
a message when an exception occurs in azure function.code :
In your code, you’re using the message receiver to settle (dead-letter) the message. Instead, you should inject
SerivceBusMessageActions
and use it to settle the message manually.