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")]
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);
}
}
catch (Exception ex)
{
await MessageReceiver.DeadLetterAsync(lockToken);
}
}
CodePudding user response:
- I was able to
deadletterqueue
a message when an exception occurs in azure function.
code :
// Servicebus Client
serviceBusClient client = new ServiceBusClient(conn);
// Creating Reciever
ServiceBusReceiver reciever = client.CreateReceiver("test");
//Downloan message
ServiceBusReceivedMessage message = await reciever.ReceiveMessageAsync();
//try - catch block for Exception
try {
// Throwing the Exception
throw new Exception();
}
catch (Exception ex) {
// Deadlettering the message
await reciever.DeadLetterMessageAsync(message);
return new OkObjectResult("Exception");
}
string g = message.Body.ToString();
return new OkObjectResult(g);
CodePudding user response:
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.