Azure Functions Bindings And Triggers

Azure Functions Bindings And Triggers
Share:

About

In this code snippet, we’ll talk about bindings and triggers in Azure Functions.

In my other post “Getting Started With C# Azure Functions”  where I show how to create a basic function I added an Http Trigger when creating the project. Here I’ll show you how to add additional triggers and bindings to your function.

Azure functions work based on triggers/bindings(which are the same thing). Bindings are used to connect your function to another resource via an input/output trigger. For example, when you add an HTTP endpoint to your function it’s really just one of input/output binding or trigger. Here you can find a list of all the other possible bindings.

Note:
You can even create custom bindings if you want. Here’s a video from Microsoft explaining how to do it.

Let’s have a look at the code below to see how to work with bindings/triggers.

Prerequisites:

Before getting started you need to install the Azure Queues and Blobs extensions NuGet packages into your project: Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues and Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs and add the following using statement at the top of your code file.
using Microsoft.Azure.Functions.Worker;

Queue Input Code Example

In this example, I’ll show you how to create a queue binding that fires when a message is added to the queue.
//This is how you can add a message queue input binding that will trigger whenever a message is added to the queue.
//In this example we are using the same storage account as the function uses.
[Function("QueueBinding")]
public void QueueBinding([QueueTrigger("test")] string myQueueItem)
{
    Console.WriteLine($"C# Queue trigger function processed: {myQueueItem}");
}

//While in this example we are using a different storage account for the queue. "OtherAccountConnection" must be defined in the local.settings.json file.
[Function("QueueBindingOtherAccountExample")]
public void QueueBindingOtherAccountExample([QueueTrigger("testOther", Connection = "OtherAccountConnection")] string myQueueItem)
{
    Console.WriteLine($"C# Queue trigger function processed: {myQueueItem}");
}
Note: Here’s where you add the connection string from the second binding.

Queue Input Binding And Blob Outpout Binding Code Example

Here we’ll add a blob output binding that will save incoming message data to .txt file that will be then uploaded to the blob storage.
//Here lets add a blob output binding that will save incoming message data to .txt file that will be then uploaded to the blob storage.
[Function("QueueBinding2")]
[BlobOutput("queue-uploads/{id}-message.txt")]
public string QueueBinding2([QueueTrigger("test")] string myQueueItem)
{
    return myQueueItem;
}
If we now add a message to the queue named “test” the binding we defined will fire get the message and upload it to the blob storage. And the great thing is we were able to implement this with just a few lines fo code.
Share:

Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger