你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
可以使用以下 Dapr 事件在 Dapr 输入绑定上触发 Azure Functions。
若要了解 Dapr 扩展的设置和配置详细信息,请参阅 Dapr 扩展概述。
Example
可以使用以下 C# 模式之一创建 C# 函数:
Execution model | Description |
---|---|
独立工作模型 | 函数代码在单独的 .NET 工作进程中运行。 与 支持的 .NET 和 .NET Framework 版本一起使用。 若要了解详细信息,请参阅 独立辅助角色模型中运行 C# Azure Functions 的指南。 |
In-process model | 函数代码与 Functions 宿主进程在同一进程中运行。 仅支持 .NET 的长期支持 (LTS) 版本。 若要了解详细信息,请参阅 使用 Azure Functions 开发 C# 类库函数。 |
[FunctionName("ConsumeMessageFromKafka")]
public static void Run(
// Note: the value of BindingName must match the binding name in components/kafka-bindings.yaml
[DaprBindingTrigger(BindingName = "%KafkaBindingName%")] JObject triggerData,
ILogger log)
{
log.LogInformation("Hello from Kafka!");
log.LogInformation($"Trigger data: {triggerData}");
}
下面是 Dapr 输入绑定触发器的 Java 代码:
@FunctionName("ConsumeMessageFromKafka")
public String run(
@DaprBindingTrigger(
bindingName = "%KafkaBindingName%")
)
使用 app
对象注册 daprBindingTrigger
:
const { app, trigger } = require('@azure/functions');
app.generic('ConsumeMessageFromKafka', {
trigger: trigger.generic({
type: 'daprBindingTrigger',
bindingName: "%KafkaBindingName%",
name: "triggerData"
}),
handler: async (request, context) => {
context.log("Node function processed a ConsumeMessageFromKafka request from the Dapr Runtime.");
context.log(context.triggerMetadata.triggerData)
}
});
The following example shows Dapr triggers in a function.json file and PowerShell code that uses those bindings.
Here's the function.json file for daprBindingTrigger
:
{
"bindings": [
{
"type": "daprBindingTrigger",
"bindingName": "%KafkaBindingName%",
"name": "triggerData",
"direction": "in"
}
]
}
For more information about function.json file properties, see the Configuration section.
In code:
using namespace System
using namespace Microsoft.Azure.WebJobs
using namespace Microsoft.Extensions.Logging
using namespace Microsoft.Azure.WebJobs.Extensions.Dapr
using namespace Newtonsoft.Json.Linq
param (
$triggerData
)
Write-Host "PowerShell function processed a ConsumeMessageFromKafka request from the Dapr Runtime."
$jsonString = $triggerData | ConvertTo-Json
Write-Host "Trigger data: $jsonString"
以下示例演示使用 v2 Python 编程模型的 Dapr 输入绑定触发器。 若要在 daprBinding
Python 函数应用代码中使用,请执行以下作:
import logging
import json
import azure.functions as func
app = func.FunctionApp()
@app.function_name(name="ConsumeMessageFromKafka")
@app.dapr_binding_trigger(arg_name="triggerData", binding_name="%KafkaBindingName%")
def main(triggerData: str) -> None:
logging.info('Python function processed a ConsumeMessageFromKafka request from the Dapr Runtime.')
logging.info('Trigger data: ' + triggerData)
Attributes
In the in-process model, use the DaprBindingTrigger
to trigger a Dapr input binding, which supports the following properties.
Parameter | Description |
---|---|
BindingName | Dapr 触发器的名称。 如果未指定,则函数的名称将用作触发器名称。 |
Annotations
通过 DaprBindingTrigger
批注,可以创建由创建的绑定组件触发的函数。
Element | Description |
---|---|
bindingName | Dapr 绑定的名称。 |
Configuration
下表说明了在代码中设置的绑定配置属性。
Property | Description |
---|---|
bindingName | 绑定的名称。 |
下表解释了在 function.json 文件中设置的绑定配置属性。
function.json property | Description |
---|---|
bindingName | 绑定的名称。 |
See the Example section for complete examples.
Usage
若要使用 Dapr 输入绑定触发器,请首先设置 Dapr 输入绑定组件。 可在 Dapr 官方文档中详细了解可使用的组件及其设置方式。