How to trigger a Service Bus

Rajamannar A K 86 Reputation points
2021-12-22T14:04:08.447+00:00

Hi all,

I have a Function app which is Service Bus queue type. Post running by logics in the code this Function App should send a message to Service Bus.

To summarize

  1. I have one function app which is a Service Bus queue type
  2. Post completion of code this should trigger a Service Bus

My doubt is on the later part of how to send message to Service Bus from Function App

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

Accepted answer
  1. MayankBargali-MSFT 70,936 Reputation points Moderator
    2021-12-23T05:09:44.7+00:00

    @Rajamannar A K As per your requirement you want to trigger your function app when the message is sent to the service bus queue so you need to add the input binding ("direction": "in") of type serviceBusTrigger. Similarly, as you want to send the message to another queue when your business logic is completed then you need to add the output binding ("direction": "out") of type serviceBusTrigger

    You can refer to Service Bus trigger Input and Service Bus Output binding for the usage and then customize it according to your need.

    function.json

    {  
      "scriptFile":"__init__.py",  
      "bindings":[  
         {  
            "name":"inputmsg",  
            "type":"serviceBusTrigger",  
            "direction":"in",  
            "queueName":"main",  
            "connection":"AzureServiceBusConnectionString"  
         },  
         {  
            "type":"serviceBus",  
            "direction":"out",  
            "connection":"AzureServiceBusConnectionString",  
            "name":"outputmsg",  
            "queueName":"outqueue"  
         }  
      ]  
    }  
    

    init.py

    import azure.functions as func  
    import logging  
    import json  
      
    def main(inputmsg: func.ServiceBusMessage,outputmsg: func.Out[str]):  
        logging.info('Python ServiceBus queue trigger processed message.')  
      
        result = json.dumps({  
            'message_id': inputmsg.message_id,  
            'body': inputmsg.get_body().decode('utf-8'),  
            'content_type': inputmsg.content_type,  
            'expiration_time': inputmsg.expiration_time,  
            'label': inputmsg.label,  
            'partition_key': inputmsg.partition_key,  
            'reply_to': inputmsg.reply_to,  
            'reply_to_session_id': inputmsg.reply_to_session_id,  
            'scheduled_enqueue_time': inputmsg.scheduled_enqueue_time,  
            'session_id': inputmsg.session_id,  
            'time_to_live': inputmsg.time_to_live,  
            'to': inputmsg.to,  
            'user_properties': inputmsg.user_properties,  
            'metadata' : inputmsg.metadata  
        }, default=str)  
      
        logging.info(result)  
      
        #you custom logic  
      
        output_msg  = 'this is my custom message'  
        outputmsg.set(output_msg)  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Erol Cakici 1 Reputation point
    2021-12-22T15:31:26.327+00:00

    Hi!
    You could do this by using output bindings functions-bindings-service-bus-output

    Or you could use a Client Library like this one for .Net


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.