title | description | services | documentationcenter | author | manager | editor | tags | keywords | ms.assetid | ms.service | ms.devlang | ms.topic | ms.tgt_pltfrm | ms.workload | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Create an Azure Function which binds to an Azure service | Microsoft Docs |
Build an Azure Function, a serverless application, which interacts with other Azure Services. |
functions |
dev-center-name |
yochay |
manager-alias |
azure functions, functions, event processing, webhooks, dynamic compute, serverless architecture |
ab86065d-6050-46c9-a336-1bfc1fa4b5a1 |
functions |
multiple |
get-started-article |
multiple |
na |
10/25/2016 |
[!INCLUDE Getting Started Note]
In this short video, you learn how to create an Azure Function that listens to messages on an Azure Queue and copies the messages to an Azure Blob.
The goal of this function is to write a message to a queue every 10 seconds. To accomplish this, you must create the function and message queues, and add the code to write messages to the newly created queues.
-
Go to the Azure Portal and locate your Azure Function App.
-
Click New Function > TimerTrigger - Node. Name the function FunctionsBindingsDemo1
-
Enter a value of "0/10 * * * * *" for the Schedule. This value is in the form of a cron expression. This schedules the timer to run every 10 seconds.
-
Click the Create button to create the function.
-
Verify that the function works by viewing activity in the log. You might have to click the Logs link in the upper right corner to display the log pane.
-
Go to the Integrate tab.
-
Choose New Output > Azure Storage Queue > Select.
-
Enter myQueueItem in the Message parameter name text box.
-
Select a storage account, or click new to create a storage account if you do not have an existing one.
-
Enter functions-bindings in the Queue name text box.
-
Click Save.
-
Return to the Develop tab, and add the following code to the function after the existing code:
function myQueueItem() { return { msg: "some message goes here", time: "time goes here" } }
-
Modify the existing function code to call the code added in Step 1. Insert the following code around line 9 of the function, after the if statement.
var toBeQed = myQueueItem(); toBeQed.time = timeStamp; context.bindings.myQueue = toBeQed;
This code creates a myQueueItem and sets its time property to the current timeStamp. It then adds the new queue item to the context's myQueue binding.
-
Click Save and Run.
-
Verify the code works by viewing the queue in Visual Studio.
- Open Visual Studio, and go to View > Cloud Explorer.
- Locate the storage account and functions-bindings queue you used when creating the myQueue queue. You should see rows of log data. You might need to sign into Azure through Visual Studio.
-
Click New Function > QueueTrigger - C#. Name the function FunctionsBindingsDemo2. Notice that you can mix languages in the same function app (Node and C# in this case).
-
Enter functions-bindings in the Queue name field."
-
Select a storage account to use or create a new one.
-
Click Create
-
Verify the new function works by viewing both the function's log and Visual Studio for updates. The function's log shows that the function is running and items are dequeued. Since the function is bound to the functions-bindings output queue as an input trigger, refreshing the functions-bindings Queue in Visual Studio should reveal that the items are gone. They have been dequeued.
-
Replace the code in FunctionsBindingsDemo2 with the following code:
using System; public static void Run(QItem myQueueItem, ICollector<TableItem> myTable, TraceWriter log) { TableItem myItem = new TableItem { PartitionKey = "key", RowKey = Guid.NewGuid().ToString(), Time = DateTime.Now.ToString("hh.mm.ss.ffffff"), Msg = myQueueItem.Msg, OriginalTime = myQueueItem.Time }; log.Verbose($"C# Queue trigger function processed: {myQueueItem.Msg} | {myQueueItem.Time}"); } public class TableItem { public string PartitionKey {get; set;} public string RowKey {get; set;} public string Time {get; set;} public string Msg {get; set;} public string OriginalTime {get; set;} } public class QItem { public string Msg { get; set;} public string Time { get; set;} }
This code adds two classes, TableItem and QItem, that you use to read and write to queues. Additionally, the Run function has been modified to accept the QItem and TraceWriter parameter, instead of a string and a TraceWriter.
-
Click the Save button.
-
Verify the code works by checking the log. Notice that Azure functions automatically serialize and deserialize the object for you, making it easy to access the queue in an object-oriented fashion to pass around data.
Now that you have the queues working together, it's time to add in an Azure table for permanent storage of the queue data.
-
Go to the Integrate tab.
-
Create an Azure Storage Table for Output and name it myTable.
-
Answer functionsbindings to the question "To which table should the data be written?".
-
Change the PartitionKey setting from {project-id} to {partition}.
-
Choose a storage account or create a new one.
-
Click Save.
-
Go to the Develop tab.
-
Create a TableItem class to represent an Azure table, and modify the Run function to accept the newly created TableItem object. Notice that you must use the PartitionKey and RowKey properties in order for it to work.
public static void Run(QItem myQueueItem, ICollector<TableItem> myTable, TraceWriter log) { TableItem myItem = new TableItem { PartitionKey = "key", RowKey = Guid.NewGuid().ToString(), Time = DateTime.Now.ToString("hh.mm.ss.ffffff"), Msg = myQueueItem.Msg, OriginalTime = myQueueItem.Time }; log.Verbose($"C# Queue trigger function processed: {myQueueItem.RowKey} | {myQueueItem.Msg} | {myQueueItem.Time}"); } public class TableItem { public string PartitionKey {get; set;} public string RowKey {get; set;} public string Time {get; set;} public string Msg {get; set;} public string OriginalTime {get; set;} }
-
Click Save.
-
Verify that the code works by viewing the function's logs and in Visual Studio. To verify in Visual Studio, use the Cloud Explorer to navigate to the functionbindings Azure Table and verify there are rows in it.
[!INCLUDE Getting Started Note]
[!INCLUDE Getting Started Note]