skip to Main Content

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


    • 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);
    
    

    enter image description here

    Login or Signup to reply.
  1. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search