Setting bool value in azure function trigger via app settings

107 Views Asked by At

is there any way I can set value of bool param StartFromBeginning from appsetting ? %StartFromBeginning% works for string not for bool

[FunctionName(nameof(MyAzureFunction))]
public async Task RunAsync([CosmosDBTrigger(
    databaseName: "myCosmosDbName",
    collectionName: "myCollectionName",
    ConnectionStringSetting = "cosmosConnectionString",
    LeaseCollectionName = "leases",
    CreateLeaseCollectionIfNotExists = true,
    MaxItemsPerInvocation = 1000,
    StartFromBeginning = true)]IReadOnlyList<Document> documents)
{
    ....
}
1

There are 1 best solutions below

0
Pravallika KV On

As far I know, it is not possible to pass the values as Boolean to the Parameters in function definition by fetching from local.settings.json because StartFromBeginning property in the CosmosDBTrigger attribute supports only constant values.

The values must be directly passed in the function definition for Boolean variables.

I have declared StartFromBeginning value in local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "demo": "AccountEndpoint=<cosmos_Conn_String>;",
    "StartFromBeginning": true
  }
}
  • I tried passing the "StartFromBeginning" variable as parameter in the function definition, but it is not allowed and leading to the error Cannot implicitly convert type string to bool . Because the value is being passed in the quotes""(as a string) to the Boolean variable.

enter image description here

Hence, it is recommended to use as shown below:

[FunctionName("Function1")]
public static void Run([CosmosDBTrigger(
    databaseName: "<database>",
    containerName: "<container>",
    Connection = "demo",
    LeaseContainerName = "leases",
    CreateLeaseContainerIfNotExists = true,
    StartFromBeginning =true)]IReadOnlyList<ToDoItem> input,

    ILogger log)
{
    if (input != null && input.Count > 0)
    {
        log.LogInformation("Documents modified " + input.Count);
        log.LogInformation("First document Id " + input[0].id);
    }
}
Functions:

        Function1: cosmosDBTrigger

For detailed output, run func with --verbose flag.
[2024-01-10T09:45:49.361Z] Executing 'Function1' (Reason='New changes on container kpccontainer at 2024-01-10T09:45:49.3151264Z', Id=afb8df08-82ff-4XXXXX7-40d73691d860)
[2024-01-10T09:45:49.384Z] Documents modified 1
[2024-01-10T09:45:49.387Z] First document Id b671723b-3132XXXXXX6fb496dcd4
[2024-01-10T09:45:49.415Z] Executed 'Function1' (Succeeded, Id=afb8df08-82XXXXX7-40d7XXX860, Duration=86ms)
[2024-01-10T09:45:51.115Z] Host lock lease acquired by instance ID '000000000000000000000000FXXXX31CC'.