แก้ไข

แชร์ผ่าน


Dapr Input Bindings trigger for Azure Functions

Azure Functions can be triggered on a Dapr input binding using the following Dapr events.

For information on setup and configuration details of the Dapr extension, see the Dapr extension overview.

Example

A C# function can be created using one of the following C# modes:

Execution model Description
Isolated worker model Your function code runs in a separate .NET worker process. Use with supported versions of .NET and .NET Framework. To learn more, see Develop .NET isolated worker process functions.
In-process model Your function code runs in the same process as the Functions host process. Supports only Long Term Support (LTS) versions of .NET. To learn more, see Develop .NET class library functions.
[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}");
}

Here's the Java code for the Dapr Input Binding trigger:

@FunctionName("ConsumeMessageFromKafka")
public String run(
        @DaprBindingTrigger(
            bindingName = "%KafkaBindingName%")
)

Use the app object to register the 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"

The following example shows a Dapr Input Binding trigger, which uses the v2 Python programming model. To use the daprBinding in your Python function app code:

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 The name of the Dapr trigger. If not specified, the name of the function is used as the trigger name.

Annotations

The DaprBindingTrigger annotation allows you to create a function that gets triggered by the binding component you created.

Element Description
bindingName The name of the Dapr binding.

Configuration

The following table explains the binding configuration properties that you set in the code.

Property Description
bindingName The name of the binding.

The following table explains the binding configuration properties that you set in the function.json file.

function.json property Description
bindingName The name of the binding.

The following table explains the binding configuration properties for @dapp.dapr_binding_trigger that you set in your Python code.

Property Description
binding_name The name of the binding.

See the Example section for complete examples.

Usage

To use the Dapr Input Binding trigger, start by setting up a Dapr input binding component. You can learn more about which component to use and how to set it up in the official Dapr documentation.

To use the daprBindingTrigger in Python v2, set up your project with the correct dependencies.

  1. Create and activate a virtual environment.

  2. In your requirements.text file, add the following line:

    azure-functions==1.18.0b3
    
  3. In the terminal, install the Python library.

    pip install -r .\requirements.txt
    
  4. Modify your local.setting.json file with the following configuration:

    "PYTHON_ISOLATE_WORKER_DEPENDENCIES":1
    

Next steps

Learn more about Dapr service invocation.