skip to Main Content

I’ve a Azure function in Python 3.11 and Linux hosting.
It is throwing an error when trigger is service bus queue message,
which is having IoTHub device connection state message with schema like this

schema.Result: Failure Exception: ValueError: cannot convert value of field 'SequenceNumber' in trigger metadata into int: invalid literal for int() with base 10: '000000000000000001dummy100000004000000000000000000, File "/azure-functions-host/workers/python/3.11/LINUX/X64/azure_functions_worker/dispatcher.py", line 570, in _handle__invocation_request args[pb.name] = bindings.from_incoming_proto

function code

@app.service_bus_queue_trigger(arg_name="azservicebus", queue_name="iothub-device-lifecycle-events",
                               connection="ServiceBusConnection")
def genaiot_servicebus_dlm_queue_trigger(azservicebus: func.ServiceBusMessage):
    logging.info('Python ServiceBus Queue trigger processed a message: %s',
                azservicebus.get_body().decode('utf-8'))

2

Answers


  1. I have tried your code and got the same error.

    The SequenceNumber is expected to be an integer, but the incoming value is a string in a hexadecimal format.

    Refer this MSDOC for Azure service bus sequence.

    In your case the error arises when it tries to convert hexadecimal string directly to an integer in base 10.

    You can retrieve and convert the SequenceNumber metadata field with below code.
    I have refered this MSDOC to receive sequence number.

    sequence_number_str = azservicebus.sequence_number
    try:
    sequence_number = int(sequence_number_str)
    logging.info('Sequence Number: %d', sequence_number)
    except ValueError as e:
    logging.error('Invalid SequenceNumber format: %s', sequence_number_str)
    
    

    The below code is with Azure Function

    app = func.FunctionApp()
    @app.service_bus_queue_trigger(
        arg_name="azservicebus", 
        queue_name="sampath",
        connection="sampath"
    )
    def servicebus_queue_trigger(azservicebus: func.ServiceBusMessage):
        try:
           
            message_body = azservicebus.get_body().decode('utf-8')
            logging.info('Python ServiceBus Queue trigger processed a message: %s', message_body)
            
           
            sequence_number_str = azservicebus.sequence_number
            try:
                
                sequence_number = int(sequence_number_str)
                logging.info('Sequence Number: %d', sequence_number)
            except ValueError as e:
                logging.error('Invalid SequenceNumber format: %s', sequence_number_str)
    
        except Exception as ex:
            logging.error('An error occurred: %s', str(ex))
    
    

    enter image description here

    Login or Signup to reply.
  2. I could see the same issue when trying to trigger a Python Function App based off the message body generated by Device Connect/Disconnect status event originating from IoT Hub.

    The message body generated by this event consists only a sequenceNumber property in the base 10 format which the causing the function processing to fail because of a formatting error which happens as the backend before entering the function code block.

    Here is a sample message body generated by the event
    enter image description here

    Instead of using IoT Hub routing, use Events under IoT Hub and filter Device Connected and Device Disconnected Events` to track the event types. Choose your Service Bus Queue as an end point. Refer the following image for set up.

    enter image description here

    Disable the route set up using IoT Hub. The events generated with this approach will have a different template. Here is a sample message body generated by such event

    enter image description here

    You can then process the event generated using the below code and get the desired data

    @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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search