Triggers and Bindings
Terminology
Trigger
- how a function is invoked
- a function must have exactly one trigger
- data associated with a trigger invocation is passed as argument(s)
Binding
- resource connections to/from a function
- can be input or output
- one function may have zero or more input and/or output bindings
Definition
Triggers and bindings are defined differently depending on development language
| Language | Configure |
|---|---|
| C# Java |
Method/parameter decorators (attributes/annotations) |
| JavaScript PowerShell Python |
editing function.json schema |
For languages which rely on the function.json schema, you can edit integrations using the portal GUI.
For statically typed languages (Java/C#), the parameter type defines the input data type. Custom type deserialisation can be done just like with ASP.NET development.
For dynamically typed languages (JavaScript, Python), use the dataType property in the function.json file.
{
"dataType": "string" (binary|stream|string),
"type": "httpTrigger",
"name": "req",
"direction": "in"
}
Binding direction
As mentioned before, bindings can be input/output. All triggers and bindings have a direction associated with them. For languages that required it, the function.json scheme accepts a property defining the direction "in" or "out".
For Java and C#, binding direction can be specified as an attribute constructor or inferred from type/usage.
Examples
Scenario
You want to write a new row to Azure Table storage whenever a new message appears in Azure Queue storage.
Using function.json schema
JSON schema defines trigger and output binding.
{
"disabled": false,
"bindings": [
{
"type": "queueTrigger",
"direction": "in",
"name": "myQueueItem",
"queueName": "myqueue-items",
"connection":"MyStorageConnectionAppSetting"
},
{
"tableName": "Person",
"connection": "MyStorageConnectionAppSetting",
"name": "tableBinding",
"type": "table",
"direction": "out"
}
]
}
C# code-first
Code decorators and definition inference define trigger and output binding.
public static class QueueTriggerTableOutput
{
[FunctionName("QueueTriggerTableOutput")]
[return: Table("outTable", Connection = "MY_TABLE_STORAGE_ACCT_APP_SETTING")]
public static Person Run(
[QueueTrigger("myqueue-items", Connection = "MY_STORAGE_ACCT_APP_SETTING")]JObject order,
ILogger log)
{
return new Person() {
PartitionKey = "Orders",
RowKey = Guid.NewGuid().ToString(),
Name = order["Name"].ToString(),
MobileNumber = order["MobileNumber"].ToString() };
}
}
public class Person
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Name { get; set; }
public string MobileNumber { get; set; }
}