Azure functions decoding error

Nayangiri Goswami 45 Reputation points
2024-09-10T07:19:11.76+00:00

Azure functions with Python language and Linux hosting are throwing this error when trigger is service bus queue message having IoTHub device connection state message schema.

Result: Failure Exception: ValueError: cannot convert value of field 'SequenceNumber' in trigger metadata into int: invalid literal for int() with base 10: '000000000000000001dummy100000004000000000000000000

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
635 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,110 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,201 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Pinaki Ghatak 4,610 Reputation points Microsoft Employee
    2024-09-11T18:35:24.32+00:00

    Hello @Nayangiri Goswami

    The error message suggests that the value of the SequenceNumber field is '000000000000000001dummy100000004000000000000000000', which is not a valid integer value.

    To resolve this issue, you can modify the code of your Azure function to handle this specific case. One way to do this is to extract the numeric part of the SequenceNumber field and convert it to an integer. Here's an example of how you can modify your code to handle this case:

    import logging
    import azure.functions as func
    def main(msg: func.ServiceBusMessage): 
      	sequence_number = msg.sequence_number 
    	# Extract the numeric part of the sequence number 
    	numeric_sequence_number = int(sequence_number.split('dummy')[0]) 
    	logging.info(f'Sequence number: {numeric_sequence_number}') 
    
    

    In this example, we extract the numeric part of the SequenceNumber field by splitting the string at the dummy substring and taking the first part. We then convert this numeric part to an integer using the 'int' function. You can then use this integer value in your function as needed. I hope this helps! Let me know if you have any further questions.

    0 comments No comments

  2. LeelaRajeshSayana-MSFT 15,966 Reputation points
    2024-09-18T15:58:16.6433333+00:00

    Hi @Nayangiri Goswami Greetings! Thank you for posting this question here.

    I could reproduce the same issue you are facing with Python application when trying to process a message triggered from Device Connection Status event generated from IoT Hub through event routing.

    I notice that the message body for the message generated through this event consists only a sequenceNumber property in the above-mentioned Base 10 format. The function app is trying to format this sequnceNumber property to an int even before entering the code which is leading to the above error.

    Instead of going with routing in IoT Hub, use Events under IoT Hub and create an Event subscription to filter Device Connected and Device Disconnected event types. Choose the end point as Service Bus Queue and point to your queue to send the events. Please find the below image for reference

    User's image

    Once you have this configured, disable any routes you may have under the IoT Hub that sends Device Connection status to the Service Bus Queue end point.

    The message delivered to the queue using the event subscription have a different template. Please find the below message body for reference

    User's image

    You can then use the below Function app code to process the message body and extract the sequence number

    @app.service_bus_queue_trigger(arg_name="azservicebus", queue_name="lsayanaiot",
                                   connection="lsayanaservicebus_SERVICEBUS") 
    def lsayana_servicebus_queue_trigger(azservicebus: func.ServiceBusMessage):
        message_body = json.loads(azservicebus.get_body().decode('utf-8'))
        logging.info('Message body is %s', message_body)
        sequence_number = message_body['data']['deviceConnectionStateEventInfo']['sequenceNumber']
        logging.info('Python ServiceBus Queue trigger processed a message: %s',
                    sequence_number)
    
    

    Hope this helps! Please let us know if you have any additional questions or need further assistance.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.