skip to Main Content

I have an Azure Function App (>=4.0 version), that has an Azure Cosmos function.

I am retrieving the value for the CreateLeaseContainerIfNotExists parameter from the environment. In Program.cs I am retrieving it into builder.Services().Bind() function.

The AppConfigClass is:

     public class AppConfigValues
     {
          public const string AppName = "BoundedContextSolution.Aggregate1NameSpace.IntegrationEventPublisher";

          public bool? CreateLeaseContainerIfNotExists { get; set; } = false;
      }

In the Function itself, I want to retrieve the value for CreateLeaseContainerIfNotExists from the environment variable of with the name of AppConfigValues.AppName plus
CreateLeaseContainerIfNotExists which would be "BoundedContextSolution.Aggregate1NameSpace.IntegrationEventPublisher.CreateLeaseContainerIfNotExists".

  public static void Run([CosmosDBTrigger(
        databaseName: "databaseName",
        containerName: "containerName",
        Connection = "",
        LeaseContainerName = "leases",
        CreateLeaseContainerIfNotExists = true)] IReadOnlyList<ToDoItem> input, 
        ILogger log]

2

Answers


  1. Chosen as BEST ANSWER

    This worked:

    CreateLeaseContainerIfNotExists = ($"{"%"}{AppConfigValues.AppName}.{EventPublisherCosmosConfiguration.EventPublisherCosmosSettings}{".CreateLeaseContainerIfNotExists%"}" != "false"))]
    

  2. CosmosDB trigger does not allow runtime values for properties like CreateLeaseContainerIfNotExists , databaseName, containerName, LeaseContainerName, Connection, as these attributes parameter is set during compile time.

    we can define the values for databaseName, containerName, LeaseContainerName, in environment variable. using "%<variable_name>%" and Connection already fetches value from environment variable.

    But CreateLeaseContainerIfNotExists requires Boolean value which cannot be defined using string value.

    By default CreateLeaseContainerIfNotExists is set to false.

    If it is set to true , it will create the lease container if no container is available with name defined in LeaseContainerName. But if Container already exists then it skips this step and uses pre-existing container.

    using System;
    using System.Collections.Generic;
    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Extensions.Logging;
    
    namespace FunctionApp5
    {
        public class Function1
        {
            private readonly ILogger _logger;
    
            public Function1(ILoggerFactory loggerFactory)
            {
                _logger = loggerFactory.CreateLogger<Function1>();
            }
    
            [Function("Function1")]
            public void Run([CosmosDBTrigger(
                databaseName: "ToDoList",
                containerName: "Items",
                Connection = "cosmosconn",
                LeaseContainerName = "Test",
                CreateLeaseContainerIfNotExists = true)] IReadOnlyList<MyDocument> input)
            {
                if (input != null && input.Count > 0)
                {
                    _logger.LogInformation("Documents modified: " + input.Count);
                    _logger.LogInformation("First document Id: " + input[0].id);
                }
            }
        }
    
        public class MyDocument
        {
            public string id { get; set; }
    
            public string Text { get; set; }
    
            public int Number { get; set; }
    
            public bool Boolean { get; set; }
        }
    }
    

    OUTPUT:

    Test container is created

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search