Search code examples
.netazureazure-functionsazure-cosmosdbazure-cosmosdb-emulator

Azure: Function App CosmosdbTrigger local development


I'm currently working on function apps and I want to use CosmosDBTrigger.

Here are my specs:

  • dotnet --version > 6.0.401
  • func --version > 4.0.4736
  • CosmosDB Emulator > azure-cosmosdb-emulator-2.14.7-c041c584
  • Windows 10 Enterprise

My issue:

 The 'blabla' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.blabla'. Microsoft.Azure.WebJobs.Extensions.CosmosDB: Cannot create Collection Information for blabla_Container in database blabla_Database with lease leases in database blabla_Database : Unable to resolve app setting for property 'CosmosDBTriggerAttribute.ConnectionStringSetting'. Make sure the app setting exists and has a valid value. Microsoft.Azure.WebJobs.Extensions.CosmosDB: Unable to resolve app setting for property 'CosmosDBTriggerAttribute.ConnectionStringSetting'. Make sure the app setting exists and has a valid value.

My Azure Function setup

 public static async Task Run(
        [CosmosDBTrigger(
        databaseName: "blabla_Database",
        collectionName: "blabla_Container",
        ConnectionStringSetting = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
        LeaseCollectionName = "leases",
        LeaseCollectionPrefix = "trigger",
        CreateLeaseCollectionIfNotExists = true
        )] string input,
        FunctionContext executionContext
        )

Host.json

    {
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    },
    "logLevel": { "default": "Trace" },
    "fileLoggingMode": "always"
  
  }
}

Local.settings.json

"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"FUNCTIONS_WORKER_RUNTIME_VERSION": "~4"

Command to run CosmosDBEmulator

.\Microsoft.Azure.Cosmos.Emulator.exe /Port=8081 /AllowNetworkAccess [/NoFirewall] /Key=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==

What works?

  • I can access the explorer without issues
  • I can create databases and containers from the explorer

So the issue appears whenever I start my functionapp and I've been digging internet to try various solutions but none of them work... Even checking my windows firewall settings... Currently I'm out of options and request help from the community.

Thanks in advance for your time.


Solution

  • The ConnectionStringSetting needs to be the name of the setting, not the value.

    For example:

     public static async Task Run(
            [CosmosDBTrigger(
            databaseName: "blabla_Database",
            collectionName: "blabla_Container",
            ConnectionStringSetting = "myCosmosDBConnection",
            LeaseCollectionName = "leases",
            LeaseCollectionPrefix = "trigger",
            CreateLeaseCollectionIfNotExists = true
            )] string input,
            FunctionContext executionContext
            )
    

    And in your settings.json file:

    {
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
        "FUNCTIONS_WORKER_RUNTIME_VERSION": "~4",
        "myCosmosDBConnection": "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
      }
    }
    

    Remember that when you deploy this Function, the local settings file does not get deployed, you need to set these configurations on the Function App Configuration.

    Also remember that the monitored container (blabla_Container) needs to exist.

    Reference: