Redaguoti

Bendrinti naudojant


Dapr Binding output binding for Azure Functions

The Dapr output binding allows you to send a value to a Dapr output binding during a function execution.

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.

The following example demonstrates using a Dapr service invocation trigger and a Dapr output binding to read and process a binding request.

[FunctionName("SendMessageToKafka")]
public static async Task Run(
    [DaprServiceInvocationTrigger] JObject payload,
    [DaprBinding(BindingName = "%KafkaBindingName%", Operation = "create")] IAsyncCollector<object> messages,
    ILogger log)
{
    log.LogInformation("C#  function processed a SendMessageToKafka request.");
    await messages.AddAsync(payload);
}

The following example creates a "SendMessagetoKafka" function using the DaprBindingOutput binding with the DaprServiceInvocationTrigger:

@FunctionName("SendMessageToKafka")
public String run(
        @DaprServiceInvocationTrigger(
            methodName = "SendMessageToKafka") 
        String payload,
        @DaprBindingOutput(
            bindingName = "%KafkaBindingName%", 
            operation = "create")
        OutputBinding<String> product,
        final ExecutionContext context) {
    context.getLogger().info("Java  function processed a SendMessageToKafka request.");
    product.setValue(payload);

    return payload;
}

In the following example, the Dapr output binding is paired with the Dapr invoke output trigger, which is registered by the app object:

const { app, trigger } = require('@azure/functions');

app.generic('SendMessageToKafka', {
    trigger: trigger.generic({
        type: 'daprServiceInvocationTrigger',
        name: "payload"
    }),
    return: daprBindingOuput,
    handler: async (request, context) => {
        context.log("Node function processed a SendMessageToKafka request from the Dapr Runtime.");
        context.log(context.triggerMetadata.payload)

        return { "data": context.triggerMetadata.payload };
    }
});

The following examples show Dapr triggers in a function.json file and PowerShell code that uses those bindings.

Here's the function.json file for daprBinding:

{
  "bindings": 
    {
      "type": "daprBinding",
      "direction": "out",
      "bindingName": "%KafkaBindingName%",
      "operation": "create",
      "name": "messages"
    }
}

For more information about function.json file properties, see the Configuration section.

In code:

using namespace System.Net

# Input bindings are passed in via param block.
param($req, $TriggerMetadata)

Write-Host "Powershell SendMessageToKafka processed a request."

$invoke_output_binding_req_body = @{
    "data" = $req
}

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name messages -Value $invoke_output_binding_req_body

The following example shows a Dapr Binding output binding, which uses the v2 Python programming model. To use @dapp.dapr_binding_output in your Python function app code:

import logging
import json
import azure.functions as func

app = func.FunctionApp()

@app.function_name(name="SendMessageToKafka")
@app.dapr_service_invocation_trigger(arg_name="payload", method_name="SendMessageToKafka")
@app.dapr_binding_output(arg_name="messages", binding_name="%KafkaBindingName%", operation="create")
def main(payload: str, messages: func.Out[bytes]) -> None:
    logging.info('Python processed a SendMessageToKafka request from the Dapr Runtime.')
    messages.set(json.dumps({"data": payload}).encode('utf-8'))

Attributes

In the in-process model, use the DaprBinding to define a Dapr binding output binding, which supports these parameters:

Parameter Description Can be sent via Attribute Can be sent via RequestBody
BindingName The name of the Dapr binding. ✔️ ✔️
Operation The configured binding operation. ✔️ ✔️
Metadata The metadata namespace. ✔️
Data Required. The data for the binding operation. ✔️

Annotations

The DaprBindingOutput annotation allows you to create a function that sends an output binding.

Element Description Can be sent via Attribute Can be sent via RequestBody
bindingName The name of the Dapr binding. ✔️ ✔️
output The configured binding operation. ✔️ ✔️
metadata The metadata namespace. ✔️
data Required. The data for the binding operation. ✔️

Configuration

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

Property Description Can be sent via Attribute Can be sent via RequestBody
bindingName The name of the binding. ✔️ ✔️
operation The binding operation. ✔️ ✔️
metadata The metadata namespace. ✔️
data Required. The data for the binding operation. ✔️

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

function.json property Description Can be sent via Attribute Can be sent via RequestBody
bindingName The name of the binding. ✔️ ✔️
operation The binding operation. ✔️ ✔️
metadata The metadata namespace. ✔️
data Required. The data for the binding operation. ✔️

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

Property Description Can be sent via Attribute Can be sent via RequestBody
binding_name The name of the binding event. ✔️ ✔️
operation The binding operation name/identifier. ✔️ ✔️
metadata The metadata namespace. ✔️
data Required. The data for the binding operation. ✔️

If properties are defined in both Attributes and RequestBody, priority is given to data provided in RequestBody.

See the Example section for complete examples.

Usage

To use the Dapr output binding, start by setting up a Dapr output 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 daprBinding 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.