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:
using Microsoft.Azure.Functions.Worker;
Queue Input Code Example
//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}"); }
Queue Input Binding And Blob Outpout Binding Code Example
//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; }