"тнιѕ вℓσg ¢συℓ∂ ѕανє уσυя мσηєу ιƒ тιмє = мσηєу" - ∂.мαηנαℓу

Friday 8 May 2020

How to Send Messages from Azure Function to Azure Service Bus Queue / Azure Service Bus Output Binding

Azure Functions provide a very easy way to send messages to Azure Service Bus Queue. Recently a colleague mentioned to me about this.So gave it a try and it worked like a charm.

I used to follow the below option. 
Aynchronously Send messages to Azure Service Bus Queue.
https://docs.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.queueclient.sendasync?view=azure-dotnet

But in Azure functions there is a cool feature called Azure Service Bus output binding. 

In simple words, Let's say I have an Azure Function ( Could be any trigger Timer, Blob etc) and from this function I would like to send a message to Azure Service Bus Queue. Instead of using queueClient.SendAsync method, I just need to bind my function to my Azure Service Queue. Azure Function would take care of the rest. This is obviously less coding and more efficient.

In the below sample code the Azure Function is a http triggered one. From this function it pushes a message to the Azure Service Bus Queue connection provided at the return part. In this case it's only sending a text message. But in a real scenario this could be a serialzed JSON. After function execution is complete, you could see the message in the Azure Service Bus Queue.

Sample Code Snippet from Microsoft :

[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;
}


Ref: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-output?tabs=csharp



No comments:

Post a Comment