Jack
Jack

Reputation: 377

How to push a message onto azure service bus within a function

I need my azure function written in C# to push a message onto the service bus. The examples I've seen online show how an azure function can be triggered when a new message happens.

Is there an example available?

Current azure function (C#)

[FunctionName("IHandleMessage")]
        public void Run([ServiceBusTrigger("my.topic", "my.subscription", Connection = "mybus_SERVICEBUS")]string mySbMsg, ILogger log)
        {

            // send new message?

        }

Many thanks! J

Update

How to create a new message within an azure function

        public void Run([ServiceBusTrigger("my.topic", "my.subscription", Connection = "mybus_SERVICEBUS")]string mySbMsg, ILogger log)
        {            
            ServiceBusOutput("hello", log);  // Create a new message
        }

        [FunctionName("AnotherEvent")]
        [return: ServiceBus("my.other.queue", Connection = "mySERVICEBUS")]
        public static string ServiceBusOutput([HttpTrigger] dynamic input, ILogger log)
        {
            log.LogInformation($"C# function processed: {input.Text}");
            return input.Text;
        }

Upvotes: 0

Views: 1309

Answers (1)

Alex
Alex

Reputation: 18526

You need to look for output binding examples.

The following example shows a C# function that sends a Service Bus queue message:

[FunctionName("ServiceBusOutput")]
[return: ServiceBus("myqueue", Connection = "ServiceBusConnection")]
public static string ServiceBusOutput([HttpTrigger] dynamic input, ILogger log)
{
    log.LogInformation($"C# function processed: {input.Text}");
    return input.Text;
}

Here's C# script code that creates multiple messages:

public static async Task Run(TimerInfo myTimer, ILogger log, IAsyncCollector<string> outputSbQueue)
{
    string message = $"Service Bus queue messages created at: {DateTime.Now}";
    log.LogInformation(message); 
    await outputSbQueue.AddAsync("1 " + message);
    await outputSbQueue.AddAsync("2 " + message);
}

Upvotes: 2

Related Questions