title | description | ms.topic | ms.date | ms.devlang | ms.custom | zone_pivot_groups |
---|---|---|---|---|---|---|
Dapr State input binding for Azure Functions |
Learn how to provide Dapr State input binding data during a function execution in Azure Functions. |
reference |
05/10/2024 |
csharp |
devx-track-csharp, devx-track-python, devx-track-dotnet, devx-track-extended-java, devx-track-js, build-2024 |
programming-languages-set-functions-lang-workers |
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.
::: zone pivot="programming-language-csharp"
A C# function can be created using one of the following C# modes:
[!INCLUDE dotnet-execution]
[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);
}
More samples for the Dapr input state binding are available in the GitHub repository.
:::code language="csharp" source="~/azure-functions-dapr-extension/samples/dotnet-isolated-azurefunction/InputBinding/StateInputBinding.cs" range="15-25":::
::: zone-end
::: zone pivot="programming-language-java"
The following example creates a "RetreveOrder"
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)
::: zone-end
::: zone pivot="programming-language-javascript"
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 JavaScript code that uses those bindings.
Here's the function.json file for daprState
:
{
"bindings":
{
"type": "daprState",
"direction": "in",
"dataType": "string",
"name": "state",
"stateStore": "statestore",
"key": "{key}"
}
}
For more information about function.json file properties, see the Configuration section.
Here's the JavaScript code:
module.exports = async function (context, req) {
context.log('Current state of this function: ' + context.bindings.daprState);
};
::: 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 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"
::: zone-end
::: zone pivot="programming-language-python"
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)
The following example shows a Dapr State input binding, which uses the v1 Python programming model.
Here's the function.json file for daprState
:
{
"scriptFile": "__init__.py",
"bindings":
{
"type": "daprState",
"direction": "in",
"dataType": "string",
"name": "state",
"stateStore": "statestore",
"key": "{key}"
}
}
For more information about function.json file properties, see the Configuration section explains these properties.
Here's the Python code:
import logging
import json
import azure.functions as func
def main(payload, data: str) -> None:
logging.info('Python function processed a RetrieveOrder request from the Dapr Runtime.')
logging.info(data)
::: zone-end
::: zone pivot="programming-language-csharp"
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. |
In the isolated worker model, use the DaprStateInput
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. |
::: zone-end
::: zone pivot="programming-language-java"
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. |
::: zone-end
::: zone pivot="programming-language-javascript, programming-language-powershell, programming-language-python"
::: zone-end
::: zone pivot="programming-language-javascript"
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 |
---|---|
stateStore | The name of the state store. |
key | The name of the key to retrieve from the specified state store. |
::: 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 |
---|---|
key | The name of the key to retrieve from the specified state store. |
stateStore | The name of the state store. |
::: zone-end
::: zone pivot="programming-language-python"
The following table explains the binding configuration properties for @dapp.dapr_state_input
that you set in your Python code.
Property | Description |
---|---|
state_store | The name of the state store. |
key | The secret key value. 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 |
---|---|
stateStore | The name of the state store. |
key | The name of the key to retrieve from the specified state store. |
::: zone-end
See the Example section for complete examples.
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.
::: zone pivot="programming-language-python"
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
The Python v1 model requires no additional changes, aside from setting up the state store.
::: zone-end