2

I have a azure python function : It gets triggered by HTTP , responds with HTTP response and puts the message in the Azure Service Bus Queue.

Function.json: for outbound Azure Service bus

{
    "type": "serviceBus",
    "connection": "ServiceBus",
    "name": "outputMessage",
    "queueName": "testqueue",
    "accessRights": "send",
    "direction": "out"
    }

I have function as

def main(req: func.HttpRequest, outputMessage:  func.Out[func.ServiceBusMessage]) -> str:

I get below error: Result: Failure Exception: FunctionLoadError: cannot load the HttpTrg function: type of outputMessage binding in function.json "serviceBus" does not match its Python annotation "ServiceBusMessage"

Question: 1. What should be the python annotation for Azure Service Bus outbound ?

def main( , outputMessage:  func.Out[func.ServiceBusMessage]) 
  1. Can I keep -> str for Azure Service Bus ?

    func.Out[func.ServiceBusMessage]) -> str

  2. Can i use the set method to send message like :

    outputMessage.set(text)

"To produce multiple outputs, use the set() method provided by the azure.functions.Out interface to assign a value to the binding" -> will this work?

Thanks Sandeep

0

3 Answers 3

0

Another example of being able to do output binding to service bus with python:

// function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
    {
      "type": "serviceBus",
      "direction": "out",
      "connection": "AzureWebJobsServiceBusConnectionString",
      "name": "msg",
      "queueName": "outqueue"
    }
  ]
}

.

# __init__.py

import azure.functions as func

def main(req: func.HttpRequest, msg: func.Out[str]) -> func.HttpResponse:

    msg.set(req.get_body())

    return 'OK'
Sign up to request clarification or add additional context in comments.

Comments

0

When you say Azure Service Queue you probably mean Azure Storage Queue.

The binding serviceBus is specifically for Service Bus, not Storage Queues.

You need to use queueName.

Here's an example of output binding. Firstly, we have the function.json file:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
  {
      "type": "queue",
      "direction": "out",
      "name": "msg",
      "queueName": "outqueue",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

Now we can use it like so:

import logging

import azure.functions as func


def main(req: func.HttpRequest, msg: func.Out[func.QueueMessage]) -> str:

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        msg.set(name)
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
            "Please pass a name on the query string or in the request body",
            status_code=400
        )

Output Binding with Python to Service Bus

Here's your function.json code:

{
    "name": "outputSbQueue",
    "type": "serviceBus",
    "queueName": "testqueue",
    "connection": "MyServiceBusConnection",
    "direction": "out"
}

Also, the following references may be helpful for you:

Azure Function - Python - ServiceBus Output Binding - Setting Custom Properties https://github.com/yokawasa/azure-functions-python-samples/blob/master/docs/quickstart-v2-python-functions.md https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python https://azure.microsoft.com/en-us/blog/taking-a-closer-look-at-python-support-for-azure-functions/

https://unofficialism.info/posts/quick-start-with-azure-function-v2-python-preview/

Sending messages to service bus queue without an explicit binding

from azure.servicebus import QueueClient, Message

# Create the QueueClient
queue_client = QueueClient.from_connection_string(
    "<CONNECTION STRING>", "<QUEUE NAME>")

# Send a test message to the queue
msg = Message(b'Test Message')
queue_client.send(msg)

11 Comments

Thanks for the reply , my bad : I am trying to post a message to Azure Service Bus Queue . I should have mentioned Service Bus , Sorry for the confusion.
Thanks, any suggestions for Azure Service Bus queue Outbound attribute?
i've updated my answer and added a few things, but havent been able to find output binding in python to servicebus queue ---- have you considered storage queues? any reason why you dont want to use storage queues?
Thanks, We have an existing application ( not Azure function ) which produces messages to Azure Service Bus . So we need a Azure function to read the message update/transform the message and put it back to Azure Service Bus queue ( different queue ) . What I have tried is slightly different HTTPTrigger -> Azure Service Bus queue ( just for understanding how azure python functions can send messages to Azure Service Bus )
i dont know if you are aware, but you dont need to have a binding in order to be able to use servicebus. the binding is just a short way of doing it.
|
0

Using the Azure ServiceBusMessage class as an outbound parameter is not supported:

Attributes are not supported by Python.
Use the Azure Service Bus SDK rather than the built-in output binding.

Azure Service Bus output binding for Azure Functions

ServiceBusMessage can only be used for Trigger binding:

import azure.functions as func
def main(msg: func.ServiceBusMessage):

The queue message is available to the function via a parameter typed as func.ServiceBusMessage. The Service Bus message is passed into the function as either a string or JSON object.

Azure Service Bus trigger for Azure Functions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.