CosmosDBTrigger not getting invoked when CosmosDB binding is also present

1.5k Views Asked by At

I'm trying to make a CosmosDBTriggered function in my precompiled C# CI/CD deployed project.

enter image description here

Here's the function implementation, which gets deployed with no complaints. I've tried static and instance methods.

enter image description here

There are no errors but also no invocations as reported by the monitoring/Insights tools even though the watched Collection has items and changes while it's deployed.

enter image description here

The function says it's enabled and has a Cosmosdb trigger:

enter image description here

I've tried adding these dependencies individually, but no changes:

<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.10" />
<PackageReference Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="1.2.1" />

This function DOES NOT appear in the Triggers of any CosmosDB Collection as I might expect, but I think that's possibly for a different kind of Trigger.

What configuration step am I missing??

UPDATE

When I comment out this [CosmosDB] DocumentClient binding (and anything that relies on it), the function is invoked. So I guess it's a problem with those bindings being used together?

enter image description here

2

There are 2 best solutions below

1
Cindy Pau On

Are you sure you set the CosmosDbConnection in azure function app on azure?

For example, this is my function app:

Function1.cs

using System;
using System.Collections.Generic;
using Microsoft.Azure.Documents;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace FunctionApp109
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([CosmosDBTrigger(
            databaseName: "testbowman",
            collectionName: "testbowman",
            ConnectionStringSetting = "str",
            CreateLeaseCollectionIfNotExists = true,
            LeaseCollectionName = "lease")]IReadOnlyList<Document> input, ILogger log)
        {
            if (input != null && input.Count > 0)
            {
                log.LogInformation("Documents modified " + input.Count);
                log.LogInformation("First document Id " + input[0].Id);
            }
        }
    }
}

local.settings.json

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx;EndpointSuffix=core.windows.net",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "str": "AccountEndpoint=https://testbowman.documents.azure.com:443/;AccountKey=xxxxxx;"
  }
}

But when deploy function app to azure, the local.settings.json will not be used, you need to set the connection string here:

enter image description here

The function app on azure will not tell you this thing, it just doesn't work.

4
Matias Quaranta On

Based on your updated post, the issue is that the Functions runtime is not initializing your Function because of some configuration issue in your bindings.

Normally, the actual error should be in your Application Insights logs.

The Cosmos DB output binding you are using is missing the collection and database properties. Checking the official samples: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-input?tabs=csharp#http-trigger-get-multiple-docs-using-documentclient-c and assuming CosmosDBConnection is a variable that points to the setting name of a setting that contains the connection string:

[CosmosDB(
                databaseName: "<your-db-name>",
                collectionName: "<your-collection-name>",
                ConnectionStringSetting = CosmosDBConnection)] DocumentClient dbClient,