|
| 1 | +--- |
| 2 | +title: Azure Functions trigger and binding example |
| 3 | +description: Learn to configure Azure Function bindings |
| 4 | +services: functions |
| 5 | +documentationcenter: na |
| 6 | +author: craigshoemaker |
| 7 | +manager: jeconnoc |
| 8 | + |
| 9 | +ms.service: azure-functions |
| 10 | +ms.devlang: multiple |
| 11 | +ms.topic: reference |
| 12 | +ms.date: 02/18/2019 |
| 13 | +ms.author: cshoe |
| 14 | +--- |
| 15 | + |
| 16 | +# Azure Functions trigger and binding example |
| 17 | + |
| 18 | +This article demonstrates how to configure a [trigger and bindings](./functions-triggers-bindings.md) in an Azure Function. |
| 19 | + |
| 20 | +Suppose you want to write a new row to Azure Table storage whenever a new message appears in Azure Queue storage. This scenario can be implemented using an Azure Queue storage trigger and an Azure Table storage output binding. |
| 21 | + |
| 22 | +Here's a *function.json* file for this scenario. |
| 23 | + |
| 24 | +```json |
| 25 | +{ |
| 26 | + "bindings": [ |
| 27 | + { |
| 28 | + "type": "queueTrigger", |
| 29 | + "direction": "in", |
| 30 | + "name": "order", |
| 31 | + "queueName": "myqueue-items", |
| 32 | + "connection": "MY_STORAGE_ACCT_APP_SETTING" |
| 33 | + }, |
| 34 | + { |
| 35 | + "type": "table", |
| 36 | + "direction": "out", |
| 37 | + "name": "$return", |
| 38 | + "tableName": "outTable", |
| 39 | + "connection": "MY_TABLE_STORAGE_ACCT_APP_SETTING" |
| 40 | + } |
| 41 | + ] |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +The first element in the `bindings` array is the Queue storage trigger. The `type` and `direction` properties identify the trigger. The `name` property identifies the function parameter that receives the queue message content. The name of the queue to monitor is in `queueName`, and the connection string is in the app setting identified by `connection`. |
| 46 | + |
| 47 | +The second element in the `bindings` array is the Azure Table Storage output binding. The `type` and `direction` properties identify the binding. The `name` property specifies how the function provides the new table row, in this case by using the function return value. The name of the table is in `tableName`, and the connection string is in the app setting identified by `connection`. |
| 48 | + |
| 49 | +To view and edit the contents of *function.json* in the Azure portal, click the **Advanced editor** option on the **Integrate** tab of your function. |
| 50 | + |
| 51 | +> [!NOTE] |
| 52 | +> The value of `connection` is the name of an app setting that contains the connection string, not the connection string itself. Bindings use connection strings stored in app settings to enforce the best practice that *function.json* does not contain service secrets. |
| 53 | +
|
| 54 | +## C# script example |
| 55 | + |
| 56 | +Here's C# script code that works with this trigger and binding. Notice that the name of the parameter that provides the queue message content is `order`; this name is required because the `name` property value in *function.json* is `order` |
| 57 | + |
| 58 | +```cs |
| 59 | +#r "Newtonsoft.Json" |
| 60 | + |
| 61 | +using Microsoft.Extensions.Logging; |
| 62 | +using Newtonsoft.Json.Linq; |
| 63 | + |
| 64 | +// From an incoming queue message that is a JSON object, add fields and write to Table storage |
| 65 | +// The method return value creates a new row in Table Storage |
| 66 | +public static Person Run(JObject order, ILogger log) |
| 67 | +{ |
| 68 | + return new Person() { |
| 69 | + PartitionKey = "Orders", |
| 70 | + RowKey = Guid.NewGuid().ToString(), |
| 71 | + Name = order["Name"].ToString(), |
| 72 | + MobileNumber = order["MobileNumber"].ToString() }; |
| 73 | +} |
| 74 | + |
| 75 | +public class Person |
| 76 | +{ |
| 77 | + public string PartitionKey { get; set; } |
| 78 | + public string RowKey { get; set; } |
| 79 | + public string Name { get; set; } |
| 80 | + public string MobileNumber { get; set; } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## JavaScript example |
| 85 | + |
| 86 | +The same *function.json* file can be used with a JavaScript function: |
| 87 | + |
| 88 | +```javascript |
| 89 | +// From an incoming queue message that is a JSON object, add fields and write to Table Storage |
| 90 | +// The second parameter to context.done is used as the value for the new row |
| 91 | +module.exports = function (context, order) { |
| 92 | + order.PartitionKey = "Orders"; |
| 93 | + order.RowKey = generateRandomId(); |
| 94 | + |
| 95 | + context.done(null, order); |
| 96 | +}; |
| 97 | + |
| 98 | +function generateRandomId() { |
| 99 | + return Math.random().toString(36).substring(2, 15) + |
| 100 | + Math.random().toString(36).substring(2, 15); |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## Class library example |
| 105 | + |
| 106 | +In a class library, the same trigger and binding information — queue and table names, storage accounts, function parameters for input and output — is provided by attributes instead of a function.json file. Here's an example: |
| 107 | + |
| 108 | +```csharp |
| 109 | +public static class QueueTriggerTableOutput |
| 110 | +{ |
| 111 | + [FunctionName("QueueTriggerTableOutput")] |
| 112 | + [return: Table("outTable", Connection = "MY_TABLE_STORAGE_ACCT_APP_SETTING")] |
| 113 | + public static Person Run( |
| 114 | + [QueueTrigger("myqueue-items", Connection = "MY_STORAGE_ACCT_APP_SETTING")]JObject order, |
| 115 | + ILogger log) |
| 116 | + { |
| 117 | + return new Person() { |
| 118 | + PartitionKey = "Orders", |
| 119 | + RowKey = Guid.NewGuid().ToString(), |
| 120 | + Name = order["Name"].ToString(), |
| 121 | + MobileNumber = order["MobileNumber"].ToString() }; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +public class Person |
| 126 | +{ |
| 127 | + public string PartitionKey { get; set; } |
| 128 | + public string RowKey { get; set; } |
| 129 | + public string Name { get; set; } |
| 130 | + public string MobileNumber { get; set; } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +You now have a working function that is triggered by Azure Table storage which outputs data to a queue. |
| 135 | + |
| 136 | +## Next steps |
| 137 | + |
| 138 | +> [!div class="nextstepaction"] |
| 139 | +> [Azure Functions binding expression patterns](./functions-bindings-expressions-patterns.md) |
0 commit comments