Skip to content

Latest commit

 

History

History
406 lines (280 loc) · 14 KB

File metadata and controls

406 lines (280 loc) · 14 KB
title Dapr Binding output binding for Azure Functions
description Learn how to provide Dapr Binding output binding data during a function execution in Azure Functions.
ms.topic reference
ms.date 05/10/2024
ms.author nigreenf
ms.reviewer hannahhunter
ms.subservice dapr
ms.devlang csharp
ms.custom devx-track-csharp, devx-track-python, devx-track-dotnet, devx-track-extended-java, devx-track-js, build-2024
zone_pivot_groups programming-languages-set-functions-lang-workers

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

::: zone pivot="programming-language-csharp"

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

[!INCLUDE dotnet-execution]

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);
}

More samples for the Dapr output invoke binding are available in the GitHub repository.

:::code language="csharp" source="~/azure-functions-dapr-extension/samples/dotnet-isolated-azurefunction/OutputBinding/SendMessageToKafka.cs" range="16-25":::


::: zone-end

::: zone pivot="programming-language-java"

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;
}

::: zone-end

::: zone pivot="programming-language-javascript"

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: daprBindingOutput,
    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 JavaScript 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.

Here's the JavaScript code:

module.exports = async function (context) {
    context.log("Node HTTP trigger function processed a request.");
    context.bindings.messages = { "data": context.bindings.args };
};

::: zone-end

::: zone pivot="programming-language-powershell"

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

::: zone-end

::: zone pivot="programming-language-python"

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'))

The following example shows a Dapr Binding output binding, which uses the v1 Python programming model.

Here's the function.json file for daprBinding:

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

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

Here's the Python code:

import logging
import json
import azure.functions as func

def main(args, messages: func.Out[bytes]) -> None:
    logging.info('Python processed a SendMessageToKafka request from the Dapr Runtime.')
    messages.set(json.dumps({"data": args}))

::: zone-end

::: zone pivot="programming-language-csharp"

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. ✔️

In the isolated worker model, use the DaprBindingOutput 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. ✔️

::: zone-end

::: zone pivot="programming-language-java"

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. ✔️

::: zone-end

::: zone pivot="programming-language-javascript, programming-language-powershell, programming-language-python"

Configuration

::: zone-end

::: zone pivot="programming-language-javascript"

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. ✔️

::: zone-end

::: zone pivot="programming-language-powershell"

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. ✔️

::: zone-end

::: zone pivot="programming-language-python"

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. ✔️

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. ✔️

::: zone-end

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.

::: zone pivot="programming-language-python"

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

The Python v1 model requires no additional changes, aside from setting up the output binding component.


::: zone-end

Next steps

Learn more about Dapr service invocation.