Dapr State input binding for Azure Functions
The Dapr state input binding allows you to read 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. |
[FunctionName("StateInputBinding")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "state/{key}")] HttpRequest req,
[DaprState("statestore", Key = "{key}")] string state,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
return new OkObjectResult(state);
}
The following example creates a "RetrieveOrder"
function using the DaprStateInput
binding with the DaprServiceInvocationTrigger
:
@FunctionName("RetrieveOrder")
public String run(
@DaprServiceInvocationTrigger(
methodName = "RetrieveOrder")
String payload,
@DaprStateInput(
stateStore = "%StateStoreName%",
key = "order")
String product,
final ExecutionContext context)
In the following example, the Dapr invoke input binding is added as an extraInput
and paired with an HTTP trigger, which is registered by the app
object:
const { app, trigger } = require('@azure/functions');
app.generic('StateInputBinding', {
trigger: trigger.generic({
type: 'httpTrigger',
authLevel: 'anonymous',
methods: ['GET'],
route: "state/{key}",
name: "req"
}),
extraInputs: [daprStateInput],
handler: async (request, context) => {
context.log("Node HTTP trigger function processed a request.");
const daprStateInputValue = context.extraInputs.get(daprStateInput);
// print the fetched state value
context.log(daprStateInputValue);
return daprStateInputValue;
}
});
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
:
{
"bindings":
{
"type": "daprState",
"direction": "in",
"key": "order",
"stateStore": "%StateStoreName%",
"name": "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, $order
)
# C# function processed a CreateNewOrder request from the Dapr Runtime.
Write-Host "PowerShell function processed a RetrieveOrder request from the Dapr Runtime."
# Convert the object to a JSON-formatted string with ConvertTo-Json
$jsonString = $order | ConvertTo-Json
Write-Host "$jsonString"
The following example shows a Dapr State input binding, which uses the v2 Python programming model. To use the daprState
binding alongside the daprServiceInvocationTrigger
in your Python function app code:
import logging
import json
import azure.functions as func
app = func.FunctionApp()
@app.function_name(name="RetrieveOrder")
@app.dapr_service_invocation_trigger(arg_name="payload", method_name="RetrieveOrder")
@app.dapr_state_input(arg_name="data", state_store="statestore", key="order")
def main(payload, data: str) :
# Function should be invoked with this command: dapr invoke --app-id functionapp --method RetrieveOrder --data '{}'
logging.info('Python function processed a RetrieveOrder request from the Dapr Runtime.')
logging.info(data)
Attributes
In the in-process model, use the DaprState
to read Dapr state into your function, which supports these parameters:
Parameter | Description |
---|---|
StateStore | The name of the state store to retrieve state. |
Key | The name of the key to retrieve from the specified state store. |
Annotations
The DaprStateInput
annotation allows you to read Dapr state into your function.
Element | Description |
---|---|
stateStore | The name of the Dapr state store. |
key | The state store key value. |
Configuration
The following table explains the binding configuration properties that you set in the code.
Property | Description |
---|---|
stateStore | The name of the state store. |
key | The name of the key to retrieve from the specified state store. |
The following table explains the binding configuration properties that you set in the function.json file.
function.json property | Description |
---|---|
key | The name of the key to retrieve from the specified state store. |
stateStore | The name of the state store. |
See the Example section for complete examples.
Usage
To use the Dapr state input 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.
In your
requirements.text
file, add the following line:azure-functions==1.18.0b3
In the terminal, install the Python library.
pip install -r .\requirements.txt
Modify your
local.setting.json
file with the following configuration:"PYTHON_ISOLATE_WORKER_DEPENDENCIES":1