Set Message Label/Subject using Azure Functions' Service Bus Output Binding

Aamir Mogra 80 Reputation points
2024-12-19T11:41:54.9366667+00:00

I am sending messages to service bus using python azure functions with the new programming model which uses output bindings like below:

@bp.service_bus_queue_output(
    arg_name="outputQueueMessage",
    queue_name="local-solar-calculation-complete-queue",
    connection="ServiceBusConnectionString"
)   

I send the message using the .set() command:

                        outputQueueMessage.set(json.dumps(response, ensure_ascii=False))

However, this only allows me to set the message body, how do I customise to include other message properties like subject/label etc.? I need these for my downstream service bus message filters to work but haven't come across a way to set anything other than the message body using the new programming model.Im aware this would have been possible using the old programming model where you create a function.json and dont use decorators.

Note - here's an image of the properties in sb explorer which im trying to set using my function app -

User's image

Thanks

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
653 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,251 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sina Salam 14,551 Reputation points
    2024-12-19T16:07:44.3633333+00:00

    Hello Aamir Mogra,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you would like to set Message Label/Subject using Azure Functions' Service Bus Output Binding using your function app.

    To avoid complexity issue while using ServiceBusClient and any issue that can lead to redundancy and potential mismanagement of resources. These are the things you can do:

    1. If custom properties are not strictly required for downstream processing, consider encoding metadata within the message body itself. This approach is simple and leverages the existing output binding but with limit customization:
         import json
         import azure.functions as func
         @bp.service_bus_queue_output(
             arg_name="outputQueueMessage",
             queue_name="local-solar-calculation-complete-queue",
             connection="ServiceBusConnectionString"
         )
         def main(req: func.HttpRequest, outputQueueMessage: func.Out[str]) -> func.HttpResponse:
             response = {
                 "data": {"key": "value"},
                 "metadata": {"subject": "your_subject", "label": "your_label"}
             }
             outputQueueMessage.set(json.dumps(response, ensure_ascii=False))
             return func.HttpResponse("Message sent with metadata encoded in the body", status_code=200)
      
      This approach keeps the decorator-based model while embedding necessary metadata in the message body for downstream services to parse.
    2. You can use manual sending for full customization, if custom properties like subject and label are indispensable, using ServiceBusClient for sending messages is the best alternative. This is an improved implementation with proper resource management and error handling:
         from azure.servicebus import ServiceBusMessage, ServiceBusClient
         import json
         import azure.functions as func
         CONNECTION_STR = "your_service_bus_connection_string"
         QUEUE_NAME = "local-solar-calculation-complete-queue"
         def main(req: func.HttpRequest) -> func.HttpResponse:
             response = {"key": "value"}  # Your message content
             message_body = json.dumps(response, ensure_ascii=False)
             try:
                 with ServiceBusClient.from_connection_string(CONNECTION_STR) as client:
                     sender = client.get_queue_sender(queue_name=QUEUE_NAME)
                     message = ServiceBusMessage(
                         body=message_body,
                         application_properties={"subject": "your_subject", "label": "your_label"}
                     )
                     sender.send_messages(message)
                 return func.HttpResponse("Message sent with custom properties", status_code=200)
             except Exception as e:
                 return func.HttpResponse(f"Error sending message: {e}", status_code=500)
      
      This method explicitly sets custom properties on the ServiceBusMessage and ensures the connection is properly closed.
    3. Finally, you can contact Azure Support if neither of the above solutions aligns with the your workflow. The feature requests can be submitted via the official Azure SDK GitHub repository - https://github.com/Azure/azure-sdk-for-python

    For more reading and details, check out the following links:

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.