Dapr State output binding for Azure Functions

The Dapr state output binding allows you to save a value to a Dapr state 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 the Dapr state output binding to persist a new state into the state store.

[FunctionName("StateOutputBinding")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = "state/{key}")] HttpRequest req,
    [DaprState("statestore", Key = "{key}")] IAsyncCollector<string> state,
    ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    await state.AddAsync(requestBody);

    return new OkResult();
}

The following example creates a "CreateNewOrderHttpTrigger" function using the DaprStateOutput binding with an HttpTrigger:

@FunctionName("CreateNewOrderHttpTrigger")
public String run(
        @HttpTrigger(
            name = "req",
            methods = {HttpMethod.POST},
            authLevel = AuthorizationLevel.ANONYMOUS)
            HttpRequestMessage<Optional<String>> request,
        @DaprStateOutput(
            stateStore = "%StateStoreName%",
            key = "product")
        OutputBinding<String> product,
        final ExecutionContext context) {
    context.getLogger().info("Java HTTP trigger (CreateNewOrderHttpTrigger) processed a request.");
}

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

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

app.generic('StateOutputBinding', {
    trigger: trigger.generic({
        type: 'httpTrigger',
        authLevel: 'anonymous',
        methods: ['POST'],
        route: "state/{key}",
        name: "req"
    }),
    return: daprStateOutput,
    handler: async (request, context) => {
        context.log("Node HTTP trigger function processed a request.");

        const payload = await request.text();
        context.log(JSON.stringify(payload));

        return { value : 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 daprState output:

{
  "bindings": 
    {
      "type": "daprState",
      "stateStore": "%StateStoreName%",
      "direction": "out",
      "name": "order",
      "key": "order"
    }
}

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 (
    $payload
)

# C# function processed a CreateNewOrder request from the Dapr Runtime.
Write-Host "PowerShell function processed a CreateNewOrder request from the Dapr Runtime."

# Payload must be of the format { "data": { "value": "some value" } }

# Convert the object to a JSON-formatted string with ConvertTo-Json
$jsonString = $payload| ConvertTo-Json

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name order -Value $payload["data"]

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

import logging
import json
import azure.functions as func

app = func.FunctionApp()

@app.function_name(name="HttpTriggerFunc")
@app.route(route="req", auth_level=dapp.auth_level.ANONYMOUS)
@app.dapr_state_output(arg_name="state", state_store="statestore", key="newOrder")
def main(req: func.HttpRequest, state: func.Out[str] ) -> str:
    # request body must be passed this way '{\"value\": { \"key\": \"some value\" } }'
    body = req.get_body()
    if body is not None:
        state.set(body.decode('utf-8'))
        logging.info(body.decode('utf-8'))
    else:
        logging.info('req body is none')
    return 'ok'

Attributes

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

Parameter Description Can be sent via Attribute Can be sent via RequestBody
StateStore The name of the state store to save state. ✔️
Key The name of the key to save state within the state store. ✔️ ✔️
Value Required. The value being stored. ✔️

Annotations

The DaprStateOutput annotation allows you to function access a state store.

Element Description Can be sent via Attribute Can be sent via RequestBody
stateStore The name of the state store to save state. ✔️
key The name of the key to save state within the state store. ✔️ ✔️
value Required. The value being stored. ✔️

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
stateStore The name of the state store to save state. ✔️
key The name of the key to save state within the state store. ✔️ ✔️
value Required. The value being stored. ✔️

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
stateStore The name of the state store to save state. ✔️
key The name of the key to save state within the state store. ✔️ ✔️
value Required. The value being stored. ✔️

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

Property Description Can be sent via Attribute Can be sent via RequestBody
stateStore The name of the state store to save state. ✔️
key The name of the key to save state within the state store. ✔️ ✔️
value Required. The value being stored. ✔️

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 state output binding, start by setting up a Dapr state store component. You can learn more about which component to use and how to set it up in the official Dapr documentation.

To use the daprState 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 state management.